Spring Integration Hibernate

Source: Internet
Author: User

Spring Integration Hibernate
1 What exactly is integrated?
I: In order to let spring's IOC container to generate sessionfactory
II: In order for Hibernate to be able to use spring's declarative transactions
2 How to integrate (integration steps)
1 Add hibernate must jar package, then add Commons-logging-1.1.3.jar, and MySQL jar package Mysql-connector-java-5.1.7-bin.jar

2 Configure Hibernate profile (the configuration of the data source does not have to be configured here, the connection database parameters are not configured here, in this file configuration database dialect, generating table policy 、、、、)

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibern Ate.sourceforge.net/hibernate-configuration-3.0.dtd "><hibernate-configuration>    <session-factory>        <!--Configure basic properties for Hibernate -        <!--1. The data source needs to be configured in the IOC container, so there is no need to configure the data source here -        <!--2. The associated. Hbm.xml is also configured in the IOC container when the Sessionfactory instance is configured -        <!--3. Configure the basic properties of Hibernate: Dialect, SQL display and formatting, strategies for generating data tables, and level two caching. -        < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQL5InnoDBDialect</property>        < Propertyname= "Hibernate.show_sql">True</ Property>        < Propertyname= "Hibernate.format_sql">True</ Property>        < Propertyname= "Hibernate.hbm2ddl.auto">Update</ Property>        <!--Configure Hibernate level two cache-related properties. -    </session-factory></hibernate-configuration>

3 in the spring configuration file configuration data source (for example, C3P0 configuration in spring, the data source is also a bean, so it is also configured with the Bean node)

<!--Configure the data source -    <!--loading Data source files -    <Context:property-placeholder Location= "Classpath:db.properties"/>    <!--Configure Data Source Properties -    <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource">        < Propertyname= "User"value= "${jdbc.user}"></ Property>        < Propertyname= "Password"value= "${jdbc.password}"></ Property>        < Propertyname= "Jdbcurl"value= "${jdbc.jdbcurl}"></ Property>        < Propertyname= "Driverclass"value= "${jdbc.driverclass}"></ Property>    </Bean>

4 Configuring Sessionfactory in the Spring configuration file
(1 tell sessionfactory which data source to use,
2 Tell Sessionfactory where the Hibernate.cfg.xml file is
3 Tell Sessionfactory where is the mapping file for the persistence class (can be placed in one place??). ))

<!--configuration sessionfactory: Configured with Spring-provided Localsessionfactorybean -    <BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <!--which data source to use -        < Propertyname= "DataSource"ref= "DataSource"></ Property>        <!--Configure the location and name of the hibernate configuration file, which is the constraint when generating sessionfactory -        < Propertyname= "Configlocation"value= "Classpath:hibernate.cfg.xml"></ Property>        <!--tell Sessionfactory where the persistence class's configuration file is -        < Propertyname= "Mappinglocations"value= "Classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml"></ Property>            </Bean>

5 Code implementation (exactly how it is used)

/*      * 1 First to obtain the Sessionfactory bar, this directly in the IOC can be, using the Autowire Annotation Association, which will automatically load Sessionfactory instance     * 2 to get the session, But the session still uses the Opensession method?? No, the thread session should be used here,     * because if it is opensession will cause a lot of sessions, will cause memory overflow and object abuse     * 3 before the session is and Hibernate's transaction (bound together), But now that we're integrated into spring, we should use spring's declarative transaction.     * So remember to call the method of the Session object, you should bind the transaction, otherwise it     will error */    autowired    private  sessionfactory sessionfactory;--obtained sessionfactory
Public session getsession () { --get session return sessionfactory.getcurrentsession () ; }

* Process of Spring hibernate transaction
* 1. Before the method starts
*①. Getting Session
*②. Bind the Session to the current thread so that you can use the sessionfactory in the Dao
* Getcurrentsession () method to get the Session
*③. Opening a transaction
*
* 2. If the method ends normally, i.e. no exception occurs, the
*①. Committing a transaction
*②. Unbind the Session that binds to the current thread
*③. Closing Session
*
* 3. If the method does not appear to be abnormal, then:
*①. Rolling back a transaction
*②. Unbind the Session that binds to the current thread
*③. Closing Session
*/

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.