Spring Integration Hibernate

Source: Internet
Author: User

Spring integrated Hibernate spring supports most popular ORM frameworks, including Hibernate JDO, TopLink, Ibatis, and JPA. •Spring's support for these ORM frameworks is consistent, and hibernate integration technology can be applied to other ORM frameworks. Spring 2.0 also supports Hibernate 2.x and 3.x. But Spring 2.5 only supports Hibernate 3.1 or later 1.Spring integer  What does Hibernate integrate? 1). There is an IOC container to manage Hibernate's sessionfactory
2). Let Hibernate use declarative transaction 2 on Spring. Integration steps:

1). Join Hibernate

①. Jar Package
②. Adding hibernate configuration files: Hibernate.cfg.xml
12<session-factory>3<!--Configure the basic properties of hibernate--4<!--1. The data source needs to be configured in the IOC container, so there is no need to configure the data source here--5<!--2. The associated. Hbm.xml is also configured in the IOC container when the Sessionfactory instance is configured-6<!--3. Configure the basic properties of Hibernate: Dialect, SQL display and formatting, strategies for generating data tables, and level two caching. -7<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</property>8 9<property name= "Hibernate.show_sql" >true</property>Ten<property name= "Hibernate.format_sql" >true</property> One          A<property name= "Hibernate.hbm2ddl.auto" >update</property> -          -<!--Configure Hibernate two cache-related properties. - the</session-factory> -sessionfactory③. Write A. hbm.xml file that corresponds to the persistence class.

2). Join Spring

①. Jar Package
②. Adding a Spring configuration file
1<!--configure automatically scanned packages--2<context:component-scan base- Package= "Com.atguigu.spring.hibernate" ></context:component-scan>3     4<!--configuring data sources-5<!--importing resource files--6<context:property-placeholder location= "Classpath:db.properties"/>7     8<bean id= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >9<property name= "user" value= "${jdbc.user}" ></property>Ten<property name= "Password" value= "${jdbc.password}" ></property> One<property name= "Driverclass" value= "${jdbc.driverclass}" ></property> A<property name= "Jdbcurl" value= "${jdbc.jdbcurl}" ></property> -  -<property name= "initialpoolsize" value= "${jdbc.initpoolsize}" ></property> the<property name= "maxpoolsize" value= "${jdbc.maxpoolsize}" ></property> -</bean> -      -<!--Configure Sessionfactory instance of Hibernate: configured with Spring-provided Localsessionfactorybean- +<bean id= "Sessionfactory"class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > -<!--Configure Data source Properties- +<property name= "DataSource" ref= "DataSource" ></property> A<!--Configure the location and name of hibernate configuration Files-- at<!-- -<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" ></property> -- -<!--use hibernateproperties zodiac to configure Hibernate native Properties- -<property name= "Hibernateproperties" > -<props> in<prop key= "Hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</prop> -<prop key= "Hibernate.show_sql" >true</prop> to<prop key= "Hibernate.format_sql" >true</prop> +<prop key= "Hibernate.hbm2ddl.auto" >update</prop> -</props> the</property> *<!--Configure the location and name of the hibernate mapping file, you can use wildcards-- $<property name= "Mappinglocations"Panax NotoginsengValue= "Classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml" ></property> -</bean> the  +<!--Configure Spring's declarative transactions-- A<!--1. Configure transaction Manager- the<bean id= "TransactionManager"class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > +<property name= "Sessionfactory" ref= "Sessionfactory" ></property> -</bean> $  $<!--2. Configuring transaction properties requires transaction manager-- -<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" > -<tx:attributes> the<tx:method name= "get*" read-only= "true"/> -<tx:method name= "Purchase" propagation= "Requires_new"/>Wuyi<tx:method name= "*"/> the</tx:attributes> -</tx:advice> Wu  -<!--3. Configure a transaction pointcut and associate the tangency point with the transaction properties- About<aop:config> $<aop:pointcut expression= "Execution (* com.atguigu.spring.hibernate.service.*.* (..))" -Id= "Txpointcut"/> -<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Txpointcut"/> -</aop:config>
Applicationcontext.xml

3). Integration.

3. Writing code

1). Use Session in Daoimpl

Private Sessionfactory sessionfactory;//does not recommend the use of hibernatetemplate and hibernatedaosupport//because this causes the Dao and Spring APIs to be coupled// Transplant-poor//private hibernatetemplate hibernatetemplate;//gets the Session that is bound to the current thread. Private Session getsession () {return sessionfactory.getcurrentsession ();}

2)

/** * Spring Hibernate transaction Process * 1.  *①. Gets the session *② before the method starts. Bind the session to the current thread so that you can use the sessionfactory * getcurrentsession () method in the Dao to get the session *③. Open transaction *  2. If the method ends normally, that is, there is no exception, then *①. Commits the transaction *②. Unbind the session with the current thread binding *③. Close Session *  * 3. If the method has an exception, then: *①. Rollback *②. Unbind the session that is bound to the current thread *③. Close Session */

Use transactions in the same way as in spring. Annotations, and declarative expressions.

Spring Integration Hibernate

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.