锘??xml version="1.0" encoding="utf-8" standalone="yes"?>POJO 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
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"<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); } }
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="
</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="
</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
路
路 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
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>