Templates believe that people who have been developed with Spring + hibernate have used the spring Hibernatedaosupport class when they write DAO, and then can easily use gethibernatetemplate when they are implemented () method, you can call Save (), delete (), update (), and so on hibernate session of the operation, very simple. Like what:
Gethibernatetemplate (). Save (user);
In this case, we have to use the following code to do this without spring:
Session session = Hibernateutil.getsession ();
Transaction tx = Session.begintransaction ();
Session.save (user);
Tx.commit ();
Hibernateutil.colsesession ();
This also eliminates exception handling, and uses the Hibernateutil class to simplify the process of getting the session from the Sessionfactory and closing the session.
But we don't necessarily use spring when we use hibernate, so we can emulate spring's approach, make a hibernate template, and use template patterns to simplify our development, and the main goal is to simplify development so that the code gets the most reuse of the words.
1. We are now implementing a hibernate Template:
Package kick.hibernate;
Import net.sf.hibernate.HibernateException;
Import net.sf.hibernate.Session;
Import net.sf.hibernate.Transaction;
public class hibernatetemplate{
public static Object run (Hibernatecallback callback) throws hibernateexception{
Session session = NULL;
Transaction tx = NULL;
try {
Session = Hibernatesessionutil.currentsession ();
tx = Session.begintransaction ();
Object result = Callback.execute (session);
Tx.commit ();
Session.flush ();
return result;
catch (Hibernateexception e) {
Tx.rollback ();
return null;
finally {
Hibernatesessionutil.closesession ();
}
}
This class is very simple, that is, using a Hibernatecallback interface to implement a fallback class, in the call to the specific requirements of the implementation of the Hibernatecallback class.
2. Back off interface Hibernatecallback:
Package kick.hibernate;
Import net.sf.hibernate.HibernateException;
Import net.sf.hibernate.Session;
Public interface Hibernatecallback {
Object execute (Session session) throws Hibernateexception;
}
OK, so we can use this template now, and we can use it in the following way:
Hibernatetemplate.run (New Hibernatecallback () {
Public Object execute throws hibernateexception {
Session.save (user);
return null;
}
});
Look, does it save a lot of code?
But it's not as simple as spring, don't worry, "bread will be there", we'll reach it.
3. To realize our own Hibernatesupport class
As you can see from the above code, we have to implement the Hibernatecallback interface ourselves, and each time we implement it, we repeat the code. So we're going to abstract and talk about these implementations into our Hibernatesupport class. Look at our code above to see that our purpose in implementing the Hibernatecallback interface is to invoke the Session.save () method, the method of the session. The code is as follows: (Click to view attachment )
4. Abstract Rootdao:
The class is an abstract class that inherits the class when it implements its own DAO class. The class has a Hibernatesupport object that uses the Gethibernatetemplate () method in the subclass to get the object and then call its corresponding method. The implementation code is as follows:
Package Kick.hibernate.dao;
Import net.sf.hibernate.Session;
Import Kick.hibernate.HibernateTemplateImpl;
Public abstract class Rootdao {
Private Hibernatesupport temp = null;
/**
* @return Returns the temp.
*/
Public Hibernatetemplateimpl Gethibernatetemplate (sessions session) {
return new Hibernatesupport ();
}
}
5. Use examples:
Define a DAO class of your own that implements the following code:
public class Userdaoimpl extends Rootdao implements userdaointerface{
public void Saveuser (user user) throws Kickexception {
Gethibernatetemplate (). saveorupdate (user);
}
................................................
To implement other methods
................................................
}