In the new version, spring and hibernate integrate the DaO layer to call the session method.

Source: Internet
Author: User

(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.

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.