锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲一区二区三区AV无码,亚洲伊人久久大香线蕉,国产亚洲精品不卡在线http://www.tkk7.com/blueoxygen/category/3744.htmlBlue keywords,Green comment,Red breakpoint,my life is also colorfulzh-cnThu, 01 Mar 2007 02:44:29 GMTThu, 01 Mar 2007 02:44:29 GMT60[瀵煎叆]Hibernate POJO's good assistant-Commons for Eclipsehttp://www.tkk7.com/blueoxygen/archive/2006/03/16/35581.htmlBlueO2BlueO2Thu, 16 Mar 2006 03:19:00 GMThttp://www.tkk7.com/blueoxygen/archive/2006/03/16/35581.htmlhttp://www.tkk7.com/blueoxygen/comments/35581.htmlhttp://www.tkk7.com/blueoxygen/archive/2006/03/16/35581.html#Feedback0http://www.tkk7.com/blueoxygen/comments/commentRss/35581.htmlhttp://www.tkk7.com/blueoxygen/services/trackbacks/35581.htmleclipsePOJO used by Hibernate needs to implement hashCode() and equals() method.That's a kind of stuffy work and will be done many many times during development.Some IDEs support automatical generation feature such as IDEA.Eclipse famous plug-in--MyElipse also suppots but it's not free of charge.
I think nearly all JAVAers know Apache Commons open source project.We can use Commons lib to generate hashCode() and equals() method.I wanna tell you that there is also a plugin for Eclipse called Commons4E which help you generate hasCode() and equals().
It also can generate toString() and compareTo() method.That's a smart plugin.Enjoy it.Link



鏂囩珷鏉ユ簮:http://blueoxygen.dflying.net/3/archive/79_hibernate_pojos_good_assistant-commons_for_eclipse.html

BlueO2 2006-03-16 11:19 鍙戣〃璇勮
]]>
[瀵煎叆]Hibernate Performance Tuninghttp://www.tkk7.com/blueoxygen/archive/2006/03/16/35579.htmlBlueO2BlueO2Thu, 16 Mar 2006 03:19:00 GMThttp://www.tkk7.com/blueoxygen/archive/2006/03/16/35579.htmlhttp://www.tkk7.com/blueoxygen/comments/35579.htmlhttp://www.tkk7.com/blueoxygen/archive/2006/03/16/35579.html#Feedback0http://www.tkk7.com/blueoxygen/comments/commentRss/35579.htmlhttp://www.tkk7.com/blueoxygen/services/trackbacks/35579.htmlHibernate and Lazy Initialization

Hibernate object relational mapping offers both lazy and non-lazy modes of object initialization. Non-lazy initialization retrieves an object and all of its related objects at load time. This can result in hundreds if not thousands of select statements when retrieving one entity. The problem is compounded when bi-directional relationships are used, often causing entire databases to be loaded during the initial request. Of course one could tediously examine each object relationship and manually remove those most costly, but in the end, we may be losing the ease of use benefit sought in using the ORM tool.

The obvious solution is to employ the lazy loading mechanism provided by hibernate. This initialization strategy only loads an object's one-to-many and many-to-many relationships when these fields are accessed. The scenario is practically transparent to the developer and a minimum amount of database requests are made, resulting in major performance gains. One drawback to this technique is that lazy loading requires the Hibernate session to remain open while the data object is in use. This causes a major problem when trying to abstract the persistence layer via the Data Access Object pattern. In order to fully abstract the persistence mechanism, all database logic, including opening and closing sessions, must not be performed in the application layer. Most often, this logic is concealed behind the DAO implementation classes which implement interface stubs. The quick and dirty solution is to forget the DAO pattern and include database connection logic in the application layer. This works for small applications but in large systems this can prove to be a major design flaw, hindering application extensibility.


Being Lazy in the Web Layer

Fortunately for us, the Spring Framework has developed an out of box web solution for using the DAO pattern in combination with Hibernate lazy loading. For anyone not familiar with using the Spring Framework in combination with Hibernate, I will not go into the details here, but I encourage you to read Hibernate Data Access with the Spring Framework. In the case of a web application, Spring comes with both the OpenSessionInViewFilter and the OpenSessionInViewInterceptor. One can use either one interchangeably as both serve the same function. The only difference between the two is the interceptor runs within the Spring container and is configured within the web application context while the Filter runs in front of Spring and is configured within the web.xml. Regardless of which one is used, they both open the hibernate session during the request binding this session to the current thread. Once bound to the thread, the open hibernate session can transparently be used within the DAO implementation classes. The session will remain open for the view allowing lazy access the database value objects. Once the view logic is complete, the hibernate session is closed either in the Filter doFilter method or the Interceptor postHandle method. Below is an example of the configuration of each component:

Interceptor Configuration

<beans> 
  <bean id="urlMapping"     
     class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    
       <property name="interceptors">
         <list>
              <ref bean="openSessionInViewInterceptor"/>
         </list>
       </property>
       <property name="mappings">
  ...
  </bean>
  ...
  <bean name="openSessionInViewInterceptor"  
    class="org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor">
       <property name="sessionFactory"><ref bean="sessionFactory"/></property>
  </bean>
</beans>
 

Filter Configuration

<web-app>
 ...      
  <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
      org.springframework.orm.hibernate.support.OpenSessionInViewFilter
    </filter-class>
   </filter>
  ...      
  <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
     <url-pattern>*.spring</url-pattern>
  </filter-mapping>
  ...
</web-app>

Implementing the Hibernate DAO's to use the open session is simple. In fact, if you are already using the Spring Framework to implement your Hibernate DAO's, most likely you will not have to change a thing. The DAO's must access Hibernate through the convenient HibernateTemplate utility, which makes database access a piece of cake. Below is an example DAO.

Example DAO

public class HibernateProductDAO extends HibernateDaoSupport implements ProductDAO  {      
 
       public Product getProduct(Integer productId) {
              return (Product)getHibernateTemplate().load(Product.class, productId);
       }
 
       public Integer saveProduct(Product product) {
              return (Integer) getHibernateTemplate().save(product);
       }       
 
       public void updateProduct(Product product) {
              getHibernateTemplate().update(product);
       }
 }

Being Lazy in the Business Layer

Even outside the view, the Spring Framework makes it easy to use lazy load initialization, through the AOP interceptor HibernateInterceptor. The hibernate interceptor transparently intercepts calls to any business object configured in the Spring application context, opening a hibernate session before the call, and closing the session afterward. Let's run through a quick example. Suppose we have an interface BusinessObject:

public interface BusinessObject { 
     public void doSomethingThatInvolvesDaos(); 
}</pre><p><font size="2">The class BusinessObjectImpl implements BusinessObject:</font></p>
<p />
<pre>public class BusinessObjectImpl implements BusinessObject {
    public void doSomethingThatInvolvesDaos() {
        // lots of logic that calls
        // DAO classes Which access 
        // data objects lazily
    }
}

Through some configurations in the Spring application context, we can instruct the HibernateInterceptor to intercept calls to the BusinessObjectImpl allowing it's methods to lazily access data objects. Take a look at the fragment below:

<beans>
    <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
         <property name="sessionFactory">
           <ref bean="sessionFactory"/>
         </property>
    </bean>
    <bean id="businessObjectTarget" class="com.acompany.BusinessObjectImpl">
       <property name="someDAO"><ref bean="someDAO"/></property>
    </bean>
    <bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean">
         <property name="target"><ref bean="businessObjectTarget"/></property>
         <property name="proxyInterfaces">
           <value>com.acompany.BusinessObject</value>
         </property>
         <property name="interceptorNames">
           <list>
              <value>hibernateInterceptor</value>
           </list>
         </property>
     </bean>            
</beans>
 
 

When the businessObject bean is referenced, the HibernateInterceptor opens a hibernate session and passes the call onto the BusinessObjectImpl. When the BusinessObjectImpl has finished executing, the HibernateInterceptor transparently closes the session. The application code has no knowledge of any persistence logic, yet it is still able to lazily access data objects.

Being Lazy in your Unit Tests

Last but not least, we'll need the ability to test our lazy application from J-Unit. This is easily done by overriding the setUp and tearDown methods of the TestCase class. I prefer to keep this code in a convenient abstract TestCase class for all of my tests to extend.

public abstract class MyLazyTestCase extends TestCase {
 
        private SessionFactory sessionFactory;
        private Session session;
	
        public void setUp() throws Exception {
	    super.setUp();
	    SessionFactory sessionFactory = (SessionFactory) getBean(\"sessionFactory&quot<img alt=";)" src="http://www.dflying.net/plugins/smileys/icons/default/wink_smile.gif" />;
	    session = SessionFactoryUtils.getSession(sessionFactory, true);
	    Session s = sessionFactory.openSession();
	    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
 
        }
 
        protected Object getBean(String beanName) {
            //Code to get objects from Spring application context
        }
	
        public void tearDown() throws Exception {
	    super.tearDown();
	    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	    Session s = holder.getSession(); 
	    s.flush();
	    TransactionSynchronizationManager.unbindResource(sessionFactory);
	    SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory);
        }
}


鏂囩珷鏉ユ簮:http://blueoxygen.dflying.net/3/archive/84_hibernate_performance_tuning.html

BlueO2 2006-03-16 11:19 鍙戣〃璇勮
]]>
Hibernate mapping summarizehttp://www.tkk7.com/blueoxygen/archive/2006/02/22/31938.htmlBlueO2BlueO2Wed, 22 Feb 2006 04:45:00 GMThttp://www.tkk7.com/blueoxygen/archive/2006/02/22/31938.htmlhttp://www.tkk7.com/blueoxygen/comments/31938.htmlhttp://www.tkk7.com/blueoxygen/archive/2006/02/22/31938.html#Feedback0http://www.tkk7.com/blueoxygen/comments/commentRss/31938.htmlhttp://www.tkk7.com/blueoxygen/services/trackbacks/31938.html

1 Entity's relation

1.1 One to One

涓涓?/SPAN>瀵硅薄鐨勪竴涓疄渚嬪搴斿彟涓涓璞$殑涓涓疄渚嬩粠鏁版嵁搴撹搴︽弿榪幫紝涓涓〃涓殑涓鏉¤褰曞搴斿彟澶栦竴涓〃涓敮涓涓鏉¤褰?/SPAN>

public class Image{
private Long id;
private String imgName;
private Blog value; //to store binary of image
......
}
Image.hbm.xml
<class name="Image" dynamic-update="true">
<id...
<property...
</class>

public class Product{
private Long id;
private String name;
private double price;
private Image photo;
}
<class name=...dynamic-update="true" lazy="true"
<id...
<property...
<one-to-one name="photo" class="package.Image" cascade="all" outer-join="auto" constrained="false"/>
</class>

1.2 Many to One

涓涓?/SPAN>瀵硅薄瀵瑰簲鍙︿竴涓璞$殑澶氫釜瀹炰緥錛屼粠鏁版嵁搴撹搴︽弿榪幫紝鏄湪涓涓暟鎹〃涓殑涓鏉¤褰曞搴斿彟澶栦竴涓暟鎹〃涓鏉¤褰?/SPAN>.

public class PersonalCompute{
private Long computerId;
private String cpu;
......
private Programmer owner;
}
<class name="Programmer" table="Programmer">
<id...
<property...
</class>

<class name="PersonalCompute" table="PCS">
<id....
<property...
<many-to-one name="owner" column="programmerId" class="Programmer">
</many-to-one>

1.3 One to Many

The same example as Many to One.But we stand at Programmer class's view point.
public class Programmer{
...
private Set computers = new HashSet();
}

<class name="Programmer" table="Programmer">
<id...
<property...
<set name="computers" inverse="true"
cascade="all">
<key column="programmerId" />
<one-to-many class="PersonalComputers">
</one-to-many>
</set>

2 Collection

2.1 Map

<class name="Team">
...
<map name="members">
<key foreign-key="fk">
<column name="teamNumber">
</key>
<index column="teamRole" type="string"/>
<element column="name" ...
</map>


showcase:
Team team1 = new Team();
team1.setName(xx);
team1.getMembers.put("index","content");
......
sess.save...

2.1.1 many-to-any

2.1.2 One to many

鍗曞崟浣跨敤Map鍋氬瓨鍌ㄥ線寰鏃犳硶琛ㄨ揪澶嶆潅鐨勫璞?/SPAN>,濡傛灉瑕佽瀵硅薄 map榪涜鏄犲皠:
One Team,multi-members
Public class Member{
...
private Team team;
}
<class name="Team">
...
<map name="members" inverse="false">
<key column="team" foreign-key="fk"/>
<index type="string" column="teamRole"/>
<one-to-many class="Member"/>
</map>
<class name="member">
<id...
<property...
<many-to-one name="team" />

showcase:
Team team = new Team();
team.setName("xx");
Member mem1 = new Member();
mem1.setXX
mem1.setXX
Member mem2 = new Member();
mem2.setXX
mem2.setXX
team.getMembers().put("xx",mem1);
team.getMembers().put("xx2",mem2);

2.1.3 many to many

<class name="Team">
<id
<propery
<map name="members" inverse="false" table="teamHasMembers">
<key column="team" foreign-key="fk"/>
<index type="string" column="teamRole"/>
<many-to-many class="member" outer-join="auto">
<column name="member"/>
</many-to-many>
</map>

<class name="Member">
<id
<property
<map name="teams" table="memberAtTeams">
<key....

teamRole is their common key of Map.
Team

id

name

2

team2

Member

di

name

age

1

davy

23

memberAtTeams

member

team

teamRole

1

2

coach

teamHasMembers

team

member

teamRole

2

1

coach

2.1.4 Composite-index

澶?/SPAN>鏉?/SPAN>Index 鍙互鏄竴涓被銆?/SPAN>
public class Position{
private String role;
private String scene;
public Position()....
getter...
setter...
}
<class name="Team">
<id...
<property...
<map name="members" inverse="false">
<key column="team" foreign-key="fk"/>
<composite-index class="Position">
<key-property..
<key-property...
</composite-index>
<one-to-many class="Member"/>
</map>
...

<class name="Member">
<id...
<property...
<many-to-one name="team"/>
</class>

index-many-to-one

index-many-to-many

index-many-to-any

2.1.5 composite-element

<import class="Member"/>
<class name="Team">
<id...
<property...
<map name="members" table="teamMembers">
<key column="teamId"/>
<index...
<composite-element class="Member">
<parent name="parent"/>
<property...
<property...
</composite-element>
</map>

2.2 Set

<class name="Team">
<..
<set name="members" inverse="false">
<key column="team" foreign-key="fk"/>
<elemnet column="name" type="string"/>
</set>

2.2.1 one-to-many

<set name="xx" column="">
<key column="" foreign-key="fk"/>
<one-to-many class="BB"/>
</set>

2.2.2 many-to-many

<class name="Team">
<id..
<property...
<set name="members" inverse="false" table="teamHasMembers">
<key column="team" foreign-key="fk">
<many-to-many class="Member" outer-join="auto">
<column name="member"/>
</set>
</class>

<class name="Member">
<id..
<property...
<set name="members" inverse="true" table="memberAtTeams">
<key column="member" foreign-key="fk">
<many-to-many class="Member" outer-join="auto">
<column name="team"/>
</set>
</class>

2.2.3 many-to-any

2.2.4 composite-element

2.3 List

<list name="members" inverse="false">
<key column="team" foreign-key="fk"/>
<index column="teamMember"/>
<element column="name" type="string"/>
</list>

2.3.1 one-to-many

2.3.2 many-to-many

2.3.3 many-to-many

2.3.4 composite-element

2.4 Bag

It can contains JDK's Collection and List interface type's object.

2.5 idBag

涓?/SPAN>bag鐩告瘮澶氬嚭浜?/SPAN>collection-id灞炴?/SPAN>

<ibdbag name="">
<collection-id column="mbid" type="long">
<generator class="hilo"/>
</collection-id>
<key..
<element..
</idbag>

2.6 array

public class Team{
//...
private String[] members;
}

<array name="members" inverse="false">
<key column="team" foreign-key="fk"/>
<index column="teamNumber"/>
<element column="name" type="string"/>
</array>

3 Component

<class name="Country">
<id..
<property...
<component name="position">
<property...
</component>
Public class Country{
//..
private Position position;
}

3.1 One-to-one

3.2 many-to-one

3.3 dynamic-component

4 Dynamic class

<dynamic-class entity-name="Country">
<id name="id" type="long">
<generator class="hilo"/>
</id>
<property name="name" column="NAME" type="string"/>
</..

4.1 One-to-one

4.2 many-to-one

5 Mapping type

5.1 Java type to SQL type

5.2 3.0 enmu

5.3 user defined data type

need implements org.hibernate.usertype.CompositeUserType or org.hibernate.usertype.UserType interface.

6 Class Hierarchy's mapping

6.1 Subclasses are saved in on Table

6.1.1 discriminator

Sapmle:

Class AbstractParent{
String id;
/*important*/
String discriminator
}
Class ChildA{
String childA;
}
Class ChildB{
String childB;
}

=========DB=========

colum name

data type

memo

id

varchar

childA

varchar

childB

varchar

class_type

varchar

used to identify subclass:A B

========mapping config file====
<class name="AbstractParent" table="PARENT" abstract="true">
<id...
<discriminator column="CLASS_TYPE" type="string"/>
<property......
<subclass name="ChildA" discriminator-value="A">
<property name="childA"/>
</subclass>
<subclass......

id generator

increment

identity

sequence

hilo

seqhilo

uuid

guid

native

assigned

foreign

composite id

<composite-id>
<key-property name=""/>
<key-property name=""/>
</composite-id>

6.2 Subclasses are saved in separated Table

騫?/SPAN>涓嶄負鐖剁被Container寤虹珛鏄犲皠鏂囦歡 Box涓?/SPAN>Bottle鐨勬槧灝勬枃浠朵篃濡傛病鏈夊叧緋諱竴鏍鋒櫘閫氱殑寤虹珛 浣嗘槸瀵?/SPAN>Container鍙栧緱鎿嶄綔

List pcs = sess.createQuery("from com.be.david.Container").list();
if(!cs.isEmpty()){
...
}
浼氬皢Box涓?/SPAN>Container鏁版嵁閮藉彇鍑烘潵 榪欐槸Hibernate榛樿闅愬紡瀹屾垚鐨?/SPAN>

6.3 Parent Class and Subclasses are saved in separated Table

<class name="Container" table="CONTAINER">
<id...
<property...
<joined-subclass name="Box" table="CONTAINER_BOX">
<key column="ContainerId"/>
<property...
</joined-subclass>
<joined-subclass name="Bottle" table="CONTAINER_BOTTLE">
<key...
<property...
</joined-subclass>
</class>

6.4 One PO multi-table

public Class Person{
鐗嬬墜鐕?/SPAN>tring id;
鐗嬬墜鐕?/SPAN>tring name;
鐗嬬墜鐕?/SPAN>tring sex;
鐗嬬墜鐕?/SPAN>tring address;
鐗嬬墜鐕?/SPAN>tring city;
鐗嬬墜鐕?/SPAN>tring zipcode;
}
we wanna save address related attributes(blue ones).Use join
<class name="Person" table="PERSON">
<id...
<property ...
<join table="ADDRESS">
鐗?/SPAN> <key column="addressID"/>
鐗?/SPAN> <property name="address"/>
鐗?/SPAN> ...
鐗?/SPAN> </join>
</class>

6.5 Class A Class B not inherited from Parent Class

6.5.1 subselect

We can use subselct to make ChildA and ChildB standalone.So how can we get all data including ChindA and ChildB?

<class name="ChildA" table="parent">
<id...
<property...
</class>
<class name="ChildB" table="parent">
<id...
<property...
</class>
<class name="Parent" mutable="false">
<subselect>
select * from ChildA
union
select * from ChildB
</subselect>
<sychronize table="ChildA"/>
<sychronize table="ChildB"/>
<id...
<property...
</class>



BlueO2 2006-02-22 12:45 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 久久aa毛片免费播放嗯啊| 国产成人一区二区三区免费视频| 亚洲中久无码永久在线观看同| 最近免费字幕中文大全| 久久精品7亚洲午夜a| 操美女视频免费网站| 中文字幕在线视频免费| 亚洲乱码卡一卡二卡三| 亚洲精品无码你懂的网站| 在免费jizzjizz在线播| 国产精品亚洲片在线花蝴蝶 | www在线观看播放免费视频日本| 久久亚洲精品视频| 91九色视频无限观看免费| 免费人成视频在线观看不卡| 久久精品视频免费播放| 久久久久亚洲AV无码专区桃色| 69视频在线观看免费| 亚洲春色在线观看| 亚洲区不卡顿区在线观看| 久久99九九国产免费看小说| aa毛片免费全部播放完整| 最新亚洲卡一卡二卡三新区| 国产aⅴ无码专区亚洲av| 永久免费av无码网站大全| 午夜免费啪视频在线观看 | 巨波霸乳在线永久免费视频| 色吊丝性永久免费看码| 亚洲中文字幕日本无线码| 亚洲AV日韩精品久久久久久| 亚洲A∨精品一区二区三区| 大地资源二在线观看免费高清 | 7723日本高清完整版免费| 丁香花在线观看免费观看图片| 亚洲欧洲免费无码| 亚洲精品91在线| 亚洲国产精品一区第二页| 亚洲精品成人在线| 国产一精品一aⅴ一免费| 男女一进一出抽搐免费视频| 亚洲日韩亚洲另类激情文学|