SSH project, what is the difference between using gethibernatetemplate and getsession? What are the pros and cons?

Source: Internet
Author: User


SSH project, what is the difference between using gethibernatetemplate and getsession? What are the pros and cons, thanks for the answer!

Reward Points: 0-resolution Time: 2008-10-7 09:42SSH project, what is the difference between using gethibernatetemplate and getsession? What are the pros and cons, thanks for the answer! Supplementary question: Thank you 451182
The main thing is to find out what the difference is between the two things. Thank you! questioner: zhongbin007 - trial level Best AnswerGethibernatetemplate has encapsulated some basic methods that can be used directly, that is, the template,
And GetSession just gets a session of the Data Factory, then most of the methods need to write their own, add HQL statements, and then use the Query method to execute

There are no advantages and disadvantages, such as the addition of delete updates such as can be directly used gethibernatetemplate and most of the conditional query needs to write with GetSession himself &&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&& &&&& Mainstream technology in the end is the mainstream technology, efficiency is not general.
Hibernate encapsulates the routine operation of the database, which is much more efficient than the simple JDBC DAO. and springframework the operation of hibernate further packaging, but also improve the efficiency of the development of a lot. The following example is the sample program that spring gives itself to the petclinic.
The original B/s architecture of a large number of database operations can be done so easily.

Package org.springframework.samples.petclinic.hibernate;

Import java.util.Collection;

Import org.springframework.dao.DataAccessException;
Import Org.springframework.orm.hibernate.support.HibernateDaoSupport;
Import Org.springframework.samples.petclinic.Clinic;
Import Org.springframework.samples.petclinic.Owner;
Import Org.springframework.samples.petclinic.Pet;
Import Org.springframework.samples.petclinic.Visit;

/**
* Hibernate implementation of the Clinic interface.
*
* <p>the mappings is defined in "Petclinic.hbm.xml",
* Located in the root of the classpath.
*
* @author Juergen Hoeller
* @since 19.10.2003
*/

PublicClass HibernateclinicExtends Hibernatedaosupport implements Clinic {

Public Collection getvets ()Throws DataAccessException {
Return Gethibernatetemplate ().
Find"From Vet Vet order by Vet.lastname, Vet.firstname");
}

Public Collection getpettypes ()Throws DataAccessException {
Return Gethibernatetemplate (). Find ("From PetType type Order by Type.Name");
}

Public Collection Findowners (String lastName)Throws DataAccessException {
Return Gethibernatetemplate ().
Find"From the owner owner where Owner.lastname like?", LastName +"%");
}

Public Owner Loadowner (int id)Throws DataAccessException {
Return (owner) gethibernatetemplate (). Load (owner.ClassNew Integer (ID));
}

Public Pet Loadpet (int id)throws dataaccessexception {
return (Pet) Gethibernatetemplate (). Load (Pet.class, new Integer ( ID));
}

public void Storeowner (owner owner) throws DataAccessException {
Gethibernatetemplate (). Saveorupdate (owner);
}

public void Storepet (Pet Pet) throws DataAccessException {
Gethibernatetemplate (). Saveorupdate (PET);
}

public void storevisit (Visit Visit) throws DataAccessException {
Gethibernatetemplate (). Saveorupdate (visit);
}

}
what does this method gethibernatetemplate () mean? Reward Points: 0-resolution Time: 2008-3-2 09:33What 's the effect? questioner: Cao Xue v - trial level Best AnswerHibernatetemplate provides a number of common ways to perform basic operations, such as the usual additions, deletions, modifications, queries, and more, Spring 2.0 adds support for named SQL queries, and also adds support for paging. In most cases, the CRUD operations of most DAO objects can be done using Hibernate's general usage. Here is an introduction to Hibernatetemplate's common methods:
Q void Delete (Object entity): Deletes the specified persisted instance
Q DeleteAll (Collection entities): Delete all persisted class instances in the collection
Q Find (String queryString): Returns an instance collection based on the HQL query string
Q findbynamedquery (String queryname): Returns an instance collection based on a named query
Q Get (class Entityclass, Serializable ID): Loads instances of a particular persisted class based on the primary key
Q Save (Object entity): Save a new instance
Q saveorupdate (Object entity): Select Save or update based on instance status
Q Update (Object entity): Updates the state of the instance, requiring the entity to be a persistent state
Q setmaxresults (int maxResults): Sets the size of the paging *********************************************

When the hibernate profile is automatically generated in the previous log, the GetSession () method is used in the DAO layer to manipulate the database records, but there is a method gethibernatetemplate (), what is the difference between the two methods?

1. Use the GetSession () method as long as you inherit Sessionfactory, and the Gethibernatetemplate () method must inherit Hibernatedaosupport of course including Sessionfactory, This difference is not particularly important, the following differences are very important

2.getSession () method is not wrapped in spring, spring will give you the most primitive session, after use, you must call the corresponding Close method, and do not manage the declarative transaction, once the connection is not closed in a timely manner, Causes the number of connections to the database connection pool to overflow, the Gethibernatetemplate () method is spring encapsulated, such as adding the appropriate declarative transaction management, which manages the corresponding connection by spring.

The actual use of the Gethibernatetemplate () is found to be much better than the GetSession () method, but some methods in gethibernatetemplate () are not provided, At this point we use the Hibernatecallback callback method to manage the database.

For example, the following code:

/**
* Operation with HQL statement
* @param hql HSQL query statement (use callback function to access external variable, must be final)
* @param offset begins to take the subscript of the data
* @param length read data record Count
* @return List result set
*/
Public List getlistforpage (final String hql, final int offset, final int length) {

List List = Gethibernatetemplate (). Executefind (New Hibernatecallback () {
Public Object Doinhibernate (session session) throws Hibernateexception, SQLException {
Query query = session.createquery (HQL);
Query.setfirstresult (offset);
Query.setmaxresults (length);
List List = Query.list ();
return list;
}
}) ;
return list;
}

SSH project, what is the difference between using gethibernatetemplate and getsession? What are the pros and cons?

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.