Simplified spring for hibernate

Source: Internet
Author: User

Simplified spring for hibernate
To access the persistence layer of hibernate, follow these steps:
(1) create a configuration instance.
(2) create a sessionfactory instance.
(3) create a session instance.
(4) Open the transaction.
(5) Start persistent access.
(6) Submit the transaction.
(7) If an exception occurs, roll back the transaction.
(8) Close the session.
Although some processes can be encapsulated using a tool class similar to hibernateutils, it is still not simple enough and requiresCodeEnable ession explicitly, start the transaction, close the transaction, and close the session. Spring provides a simpler way to operate the persistent layer. You do not need to open the session explicitly or execute any transaction operation statements in the code.
Spring provides the hibernatetemplate for persistent layer access. This template class does not need to show session opening or closing session. As long as it gets the reference of sessionfactory, it can intelligently open the session and close the session after the persistent access ends,ProgramDevelopment only needs to complete the persistence layer logic, and general operations are completed by hibernatetemplate.

The transaction processing, of course, can also adopt programming-type transactions. Spring provides support for programmatic transactions. We recommend that you use declarative transactions. Declarative transactions have the following advantages:
· The Code does not need to implement any transaction logic. program developers can focus more on the implementation of business logic.
· Declarative transactions are not associated with any transaction policy. Declarative transactions can be easily switched between global transactions and local transactions.
Spring declarative transactions are based on Spring's AOP. Developers do not need to have a deep understanding of AOP. They only need to configure declarative transaction proxies according to the sections later in this chapter.
Spring simplifies Hibernate and also benefits from spring exception handling policies. Spring believes that the underlying database exceptions are almost irrecoverable, and it makes almost no sense to forcibly handle the underlying database. However, the traditional JDBC database access exceptions are checked exceptions, so try must be used... and catch block processing.
In addition, spring encapsulates hibernate exceptions and converts them to the dataaccessexception inheritance tree. All dataaccessexception is a runtime exception, but it does not force capture. In summary, spring simplifies hibernate in the following aspects.
· Based on the sessionfactory Management Mechanism of dependency injection, sessionfactory is the core component for executing persistence operations. In traditional hibernate applications, sessionfactory must be manually created. With dependency injection, the Code does not need to care about sessionfactory. beanfactory is responsible for the creation and maintenance of sessionfactory.
· Better session management mechanism. Spring provides a "session once per transaction" mechanism, which greatly improves the system performance. In addition, spring manages sessions transparently without having to operate sessions in the code.
· Unified transaction management. Spring provides consistent programming models for both programmatic and declarative transactions, without the hassle of starting transactions, explicit commit, and rollback. If declarative transaction management is used, the transaction management logic and code can be separated so that transactions can be switched between global transactions and local transactions.
· Unified exception handling mechanism. Developers are no longer forced to capture exceptions on the persistence layer. Generally, exceptions on the persistence layer are encapsulated as child classes of dataaccessexception exceptions, and underlying database exceptions are encapsulated as business exceptions, developers can decide to handle exceptions at the appropriate layer.
• Hibernatetemplate supports classes. Hibernatetempate can perform a large number of hibernate persistent layer operations. Most of these operations only require some simple code.

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.