1:get/load access to single data
Public Teacher Getteacherbyid (Long ID) {return
(Teacher) this.hibernateTemplate.get (Teacher.class, id);
Public Teacher Getteacherbyid (Long ID) {return
(Teacher) this.hibernateTemplate.load (Teacher.class, id);
2:find/iterate Query Operations
Public iterator getteachersbyage (int age) {
iterator iterator = null;
Use the Find method
List List = (list) this.hibernatetemplate (). Find ("from Teacher t where T.age>?", new Integer); C10/>iterator = List.iterator ();
Use the iterator method
iterator = This.hibernatetemplate (). Iterate ("from Teacher t where T.age>?", new Integer); C13/>return iterator;
}
Find and Iterato The difference is mainly iterate used n+1 query, for large quantities of inquiries, such as Query 10,000 records, then iterate will perform 10000+1 query, find and iterate should be based on specific actual
Situation to use, for frequent write operations objects, you should use the Find query, and for some read-only data objects, you should use the iterate operation, because the iterate operation uses the Hibernate caching mechanism
3:save/update/saveorupdate/delete Save/Update/delete operation
public void Save (Teacher Teacher) {
this.hibernateTemplate.save (Teacher);
}
public void Update (Teacher Teacher) {
this.hibernateTemplate.update (Teacher);
}
public void Update (Teacher Teacher) {
this.hibernateTemplate.saveOrUpdate (Teacher);
}
public void Update (Teacher Teacher) {
this.hibernateTemplate.delete (Teacher);
}
4:bulkupdate Bulk Delete or update
Bulkupdate provides bulk deletion and update, direct conversion to corresponding Update/delete SQL for bulk deletion and update
public void Batchdelete (String name, int age) {
this.hibernateTemplate.bulkUpdate ("Delete Teacher where name=?") and age =? ', new Object[]{name, Age});
public void Batchdelete (string name, String newName) {
this.hibernateTemplate.bulkUpdate ("Update Teacher set name=?") Where Name=? ", New Object[]{newname, name});
One problem to note at this point is that using the bulkupdate operation, you must manually clear the cache of related objects in Hibernate (including the first-level cache and level two cache)
5:execute Core Approach
Public Object Execute (hibernatecallback action, Boolean exposenativesession) throws DataAccessException {//Get a session
Session session = GetSession (); Whether the current session is in the transaction Boolean existingtransaction = Sessionfactoryutils.issessiontransactional (Session,
Getsessionfactory ());
Flushmode previousflushmode = null;
try {Previousflushmode = Applyflushmode (session, existingtransaction);//Apply Flush mode Enablefilters (session);
Session Session Sessiontoexpose = (exposenativesession. Session:createsessionproxy (session)) exposed to the action;
Execute action Object result = Action.doinhibernate (sessiontoexpose);
Flushifnecessary (session, Existingtransaction);
return result;
catch (Hibernateexception ex) {throw converthibernateaccessexception (ex);
catch (SQLException ex) {throw convertjdbcaccessexception (ex);
catch (RuntimeException ex) {throw ex; finally {//if session is in a transaction, do not close session if (Existingtransaction) {disablefilters (SessiON);
if (Previousflushmode!= null) {Session.setflushmode (Previousflushmode);
} else {//release session sessionfactoryutils.releasesession (Session, Getsessionfactory ()); }
}
}
*hibernatecallback, typically used to implement specific business logic
*exposenativesession: Is a Boolean value that you want to expose to hibernatecallback actual session object, not a proxy object
6: In general, the Execute method is used only if the method provided by Hiberatetemplate does not meet the requirements, and it is used as follows.
public void Createdatabaseschema () throws DataAccessException {
Hibernatetemplate hibernatetemplate = new Hibernatetemplate (this.sessionfactory);
Invoke the Hibernatetempalte Execute method
Hibernatetemplate.execute (new Hibernatecallback () {public
Object Doinhibernate (Session session) throws Hibernateexception, SQLException { //implementation Hibernatecallback Doinhibernate method
//Specific implementation
Connection conn = Session.connection ();
Final dialect dialect = dialect.getdialect (configuration.getproperties);
string[] sql = configuration.generateschemacreationscript (dialect);
EXECUTESCHEMASCRIPT (conn, SQL);}}
);
The key to using the Execute method is to implement the Hibernatecallback Doinhibernate method, which passes a session instance that you can use to manipulate the database. The benefit of the Execute method is that the application does not care about the creation and release of the session, only the business logic concerned.