(From: Beginning Hibernate and spring-framework-3.0.5.RELEASE reference)
In the spring and hibernate integration framework, session factory is configured as a spring
Bean. in earlier versions, the hibernatedaosupport and hibernatetemplate classes are used. methods can be directly used in the new version.
The getcurrentsession () of session factory returns the Session of the current transaction.
For example:
1. public list <Paper> getall () {<br/> 2. session session = This. sessionfactory. getcurrentsession (); <br/> 3. list <Paper> papers = (list <Paper>) session. createquery ("from paper "). list (); <br/> 4. return papers; <br/> 5 .}Hibernate 3.0.1 has the contextual Sessions feature, so that hibernate manages the current session for each transaction. This feature was previously implemented by spring-related support classes. We recommend this method. Advantage: It only depends on Hibernate and does not need spring-related classes. Reduce scalability. (Hibernate 3 has a feature called contextual sessions, wherein
Hibernate itself manages one current
Session
Per transaction. This is roughly
Equivalent to spring's synchronization of one hibernate
Session
Per transaction. A corresponding
DAO implementation resembles the following example, based on the plain
Hibernate API :) spring's localsessionfactorybean supports the hibernate
Sessionfactory. getcurrentsession () method, returns the current spring-managed transaction session, (Spring's localsessionfactorybean supports hibernate's
Sessionfactory. getcurrentsession () method for any Spring transaction strategy, returning
The current spring-managed transactional session even with hibernatetransactionmanager .)
Public list <Paper> getall () {<br/> session = This. sessionfactory. getcurrentsession (); <br/> List <Paper> papers = (list <Paper>) session. createquery ("from paper "). list (); <br/> return papers;
With your session factory configured as a spring bean, you can now go on to create daos that take
Advantage of hibernate's functionality. Previous versions of spring and hibernate required the use
The hibernatedaosupport class and/or hibernatetemplate class to form the basis of your daos; however,
Recent versions of spring and hibernate have eliminated the need for these classes. hibernate now
Supports a getcurrentsession () method on the sessionfactory that returns a session object that is
Associated with the current transaction.