Hibernatetemplate in development-type spring

Source: Internet
Author: User

I believe I have used spring +
Hibernate developers used the hibernatedaosupport class of spring when writing Dao.
You can use the gethibernatetemplate () method to call save (), delete (), update (), and so on.
Hibernate session operations are very simple. For example:
Gethibernatetemplate (). Save (User );
In this case, the following code must be used before we can complete the process without spring:
Session session = hibernateutil. getsession ();
Transaction Tx = session. begintransaction ();
Session. Save (User );
TX. Commit ();
Hibernateutil. colsesession ();
Exception Handling is also saved, and the hibernateutil class is used to simplify the process of getting sessions from sessionfactory and closing sessions.
However, we may not necessarily use spring when using hibernate, so we can simulate the processing method of spring, make a hibernate template, and use the template mode to simplify our development, its main purpose is to simplify development and reuse code to the greatest extent.
1. Now we implement 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.exe cute (session );
TX. Commit ();
Session. Flush ();
Return result;
} Catch (hibernateexception e ){
TX. rollback ();
Return NULL;
} Finally {
Hibernatesessionutil. closesession ();
}
}
The class here is very simple, that is, using a callback class that implements the hibernatecallback interface, and implementing the hibernatecallback class according to specific requirements during the call.
2. Return the interface hibernatecallback:
Package kick. hibernate;

Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. Session;

Public interface hibernatecallback {
Object execute (session) throws hibernateexception;
}
Now, we can use this template as follows:
Hibernatetemplate. Run (New hibernatecallback (){
Public object execute (session) throws hibernateexception {
Session. Save (User );
Return NULL;
}
});
Check whether a lot of code is saved? Pai_^
However, this is not as simple as spring. Don't worry, "There will be bread." Oh, we will.
3. implement our own hibernatesupport class:
From the code above, we can see that we need to implement the hibernatecallback interface by ourselves, and the code is repeated every time we implement it. Therefore, let's abstract these implementations into us.
In the hibernatesupport class. Look at the code above and we will know that the purpose of implementing the hibernatecallback interface is to call
Session. Save () method, that is, the session method. The Code is as follows:
Package kick. hibernate;

Import java. Io. serializable;

Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. Session;

Public class hibernatesupport {

Public object save (final object) throws hibernateexception {
Return hibernatetemplate. Run (New hibernatecallback (){

Public object execute (session) throws hibernateexception {
Session. Save (object );
Return NULL;
}

});
}
Public object save (final object, final serializable ID) throws hibernateexception {
Return hibernatetemplate. Run (New hibernatecallback (){

Public object execute () throws hibernateexception {
Session. Save (object, ID );
Return NULL;
}

});
}

Public object saveorupdate (final object) throws hibernateexception {
Return hibernatetemplate. Run (New hibernatecallback (){

Public object execute (session) throws hibernateexception {
Session. saveorupdate (object );
Return NULL;
}

});
}
................................................................................................
................................................................................................
................................................................................................

Call other session methods.
}
4. Abstract rootdao:
This class is an abstract class and inherits this class when implementing its own DAO class. This class has a hibernatesupport object. You can use the gethibernatetemplate () method in the subclass to obtain 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 (session ){
Return new hibernatesupport ();
}
}
5. Example:
Define your own DAO class. The implementation code is as follows:
Public class userdaoimpl extends rootdao implements userdaointerface {
Public void saveuser (User user) throws kickexception {
Gethibernatetemplate (). saveorupdate (User );
}
................................................................................................
Implement other methods
................................................................................................
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.