/**
Author: willpower
Source: rifoo Technology (http://www.rifoo.com)
Time: 2006-02-19
Remarks: Reprinted please keep the above statement
**/
Spring developers are increasingly using hibernate as a persistent layer framework. Although spring can also integrate persistence layer frameworks such as JDO and ibatis, Hibernate is the most widely used. These two lightweight frameworks can be well coordinated and integrated with each other. Next, let's take a look at how spring and Hibernate are integrated.
Hibernate is an excellent persistent layer framework. Currently, it is very popular, and it is faster than other frameworks and free of charge. It provides a large number of ing support and convenient models, making it highly respected. Hibernate is especially useful for small and medium-sized projects. However, we do not have to use hibernate. It has not become a standard, but it is currently the most popular.
Next we will configure spring to Use ORM (Object link ing ). We need to copy the following files from the DIST directory of spring (Other sources can also) to Lib in the project file: hibernate2.jar, aopalliance. jar, cglib-full-2.0.2.jar, dom4j. jar, ehcache-1.1.jar and ODMG. jar.
Since hibernate uses Java reflection technology, the performance of bytecode is not greatly improved. What we need to do to make the model persistent is to create mappings and reference them in the context.
The code for the ing file is as follows:
Example 5-12. Bike. HBM. xml
<Hibernate-mapping>
<Class name = "com. Springbook. Bike" table = "bikes">
<ID name = "bikeid" column = "bikeid" type = "Java. Lang. Integer"
Unsaved-value = "-1">
<Generator class = "native"> </generator>
</ID>
<Property name = "manufacturer" column = "manufacturer" type = "string"/>
<Property name = "model" column = "model" type = "string"/>
<Property name = "frame" column = "frame" type = "int"/>
<Property name = "serialno" column = "serialno" type = "string"/>
<Property name = "weight" column = "weight" type = "Java. Lang. Double"/>
<Property name = "status" column = "status" type = "string"/>
<Set name = "Reservations">
<Key column = "bikeid"/>
<One-to-learn class = "com. Springbook. Reservation"/>
</Set>
</Class>
</Hibernate-mapping>
Example 5-13. Customer. HBM. xml
<Hibernate-mapping>
<Class name = "com. Springbook. Customer" table = "customers">
<ID name = "custid" column = "custid" type = "Java. Lang. Integer"
Unsaved-value = "-1">
<Generator class = "native"> </generator>
</ID>
<Property name = "firstname" column = "firstname" type = "string"/>
<Property name = "lastname" column = "lastname" type = "string"/>
<Set name = "Reservations">
<Key column = "custid"/>
<One-to-learn class = "com. Springbook. Reservation"/>
</Set>
</Class>
</Hibernate-mapping>
Example 5-14. Reservation. HBM. xml
<Hibernate-mapping>
<Class name = "com. Springbook. Reservation" table = "Reservations">
<ID name = "reservationid" column = "resid" type = "Java. Lang. Integer"
Unsaved-value = "-1">
<Generator class = "native"> </generator>
</ID>
<Property name = "reservationdate" column = "resdate" type = "date"/>
<Subtitle-to-one name = "Bike" column = "bikeid" class = "com. Springbook. Bike"
Cascade = "NONE"/>
<Role-to-one name = "customer" column = "custid"
Class = "com. Springbook. Customer"
Cascade = "NONE"/>
</Class>
</Hibernate-mapping>
In the context, we need to configure hibernate properties, configure the session factory, and add the session factory as a plug-in to the facade. In terms of transaction processing, JDO, ibatis are the same as Hibernate, and are completed by dependency injection.
The following code shows the changes to the context file:
Example 5-15. RentABikeApp-servlet.xml
<Bean name = "rentabike" class = "com. Springbook. hibtoffabike">
<Property name = "storename"> <value> Bruce's bikes </value> </property>
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean>
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate. localsessionfactorybean">
<Property name = "datasource"> <ref local = "datasource"/> </property>
<Property name = "mappingresources">
<List>
<Value> COM/Springbook/bike. HBM. xml </value>
<Value> COM/Springbook/customer. HBM. xml </value>
<Value> COM/Springbook/Reservation. HBM. xml </value>
</List>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect">
Net. SF. hibernate. dialect. mysqldialect
</Prop>
<Prop key = "hibernate. show_ SQL"> true </prop>
</Props>
</Property>
</Bean>
Because we have completed a facade interface before, now we only need a hibernate implementation class to implement this facade interface. We can use templates technology. Specify an hql statement for the search method. For updates, specify a new object for storage.
The following is the code for implementing the facade class in hibernate:
Example 5-16. hibateabike. Java
Package com. Springbook;
Import org. springframework. Orm. hibernate. Support. hibernatedaosupport;
Import java. util. List;
Import java. util. date;
Import java. util. Set;
Import net. SF. hibernate. query;
Public class hibateabike extends hibernatedaosupport implements extends abike {
Private string name;
Public list getbikes (){
Return gethibernatetemplate (). Find ("from bike ");
}
Public bike getbike (string serialno ){
Bike B = NULL;
List bikes = gethibernatetemplate ().
Find ("from bike Where serialno =? ", Serialno );
If (bikes. Size ()> 0 ){
B = (bike) bikes. Get (0 );
}
Return B;
}
Public bike getbike (INT bikeid ){
Return (bike) gethibernatetemplate ().
Load (bike. Class, new INTEGER (bikeid ));
}
Public void savebike (bike ){
Gethibernatetemplate (). saveorupdate (bike );
}
Public void deletebike (bike ){
Gethibernatetemplate (). Delete (bike );
}
Public void setstorename (string name ){
This. Name = Name;
}
Public String getstorename (){
Return this. Name;
}
Public list getcustomers (){
Return gethibernatetemplate (). Find ("from customer ");
}
Public customer getcustomer (INT custid ){
Return (customer) gethibernatetemplate ().
Load (customer. Class, new INTEGER (custid ));
}
Public list getreservations (){
Return gethibernatetemplate (). Find ("from Reservation ");
}
Public list getreservations (customer ){
Return gethibernatetemplate ().
Find ("from reservation where custid =? ", Customer. getcustid ());
}
Public list getreservations (bike ){
Return gethibernatetemplate ().
Find ("from reservation where bikeid =? ", Bike. getbikeid ());}
Public list getreservations (date ){
Return gethibernatetemplate ().
Find ("from reservation where resdate =? ", Date );
}
Public reservation getreservation (INT resid ){
Return (reservation) gethibernatetemplate ().
Load (reservation. Class, new INTEGER (resid ));}
}
Next, let's take a look at the traditional way of writing hibernate, that is, when it is not integrated with spring:
Example 5-17. hibateabike. Java
Public list getbikesoldway () throws exception {
// Relies on other static code for configuration
// And generation of sessionfactory. might look like:
// Configuration Config = new configuration ();
// Config. addclass (bike. Class). addclass (customer. class ).
// Addclass (reservation. Class );
// Sessionfactory mysessionfactory = configuration.
// Buildsessionfactory ();
List bikes = NULL;
Session S = NULL;
Try {
S = mysessionfactory. opensession ();
Bikes = S. Find ("from bike ");
} Catch (exception ex ){
// Handle exception gracefully
} Finally {
S. Close ();
}
Return bikes;
}
Spring's hibernate template processing method is almost the same as Hibernate's traditional method:
Example 5-18. hibateabike. Java
Public list getbikes (){
Return gethibernatetemplate (). Find ("from bike ");
}
We can see that in spring, writing such code is much less, and more things about transaction processing are completed by spring. Our programmers only need to pay attention to business implementation.