SSH (vi) Hibernate persistence layer template in transaction management

Source: Internet
Author: User

Persistent layer as long as the completion of data on the database additions and deletions to the operation, we often say that hibernate is different from MyBatis is in his automatic, and Hibernate's automatic is mainly reflected in

His template, some simple data manipulation we will not have to write the SQL, can be done by the configuration of Hibernate template.

First, inject hibernate template in DAO

1.1 Inherit the hibernate template provided by spring in the DAO layer

Modify the Entity product class so that he inherits Hibernatedaosupport

1.2 Configuring the DAO Layer injection hibernate template in Applicationcontext.xml

        <!--Configure DAO Layer: Inject Hibernate template--        <bean id= "Productdao" class= "Com.ssh.dao.productDao" >        <!--ref The value is consistent with the Sessionfactory Bean ID--        <property name= "sessionfactory" ref= "Sessionfactory" ></property >        </bean>

1.3 The Hibernate template is called at the DAO layer to complete the data operation: (Template calls This.gethibernatetemplate ().)

public void Sava (product product) {SYSTEM.OUT.PRINTLN ("Business layer invoke DAO layer succeeded!") ");//Call Hibernate template to complete save Data Operation This.gethibernatetemplate (). Save (product);

Second, add transaction management

2.1 Configuring Transaction Management

        <!--configuration transaction Manager-        <bean id= "TransactionManager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager ">        <property name=" sessionfactory "ref=" Sessionfactory "></property>        </bean>

2.2 Open Transaction Management

        <!--open annotation transaction--        <tx:annotation-driven transaction-manager= "TransactionManager"/>

2.3 Introducing transaction management mechanisms at the business level @transactional

SSH (vi) Hibernate persistence layer template in transaction management

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.