8--deep use of spring--8...2 managing Hibernate sessionfactory

Source: Internet
Author: User

8.8.2 managing Hibernate's Sessionfactory

When you make persistent layer access through Hibernate, you must first obtain the Sessionfactory object, which is the compiled memory image of a single database mapping relationship. In most cases, a Java EE application corresponds to a database, which corresponds to a Sessionfactory object.

Spring's IOC container can not only configure Sessionfactory instances in a declarative manner, but also take full advantage of the role of IOC containers and inject data source references for Sessionfactory.

Configuring Hibernate sessionfactory in the xml:spring configuration file

<?XML version= "1.0" encoding= "UTF-8"?><!--the root element of the Spring configuration file, using spring-beans-4.0.xsd semantics constraints -<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:util= "Http://www.springframework.org/schema/util"xsi:schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ spring-beans-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/ Spring-util-4.0.xsd ">    <!--define the data source bean, implement it using the C3P0 data source, and inject the necessary information into the data source -    <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"Destroy-method= "Close">        < Propertyname= "Driverclass"value= "Com.mysql.jdbc.Driver"/>        < Propertyname= "Jdbcurl"value= "Jdbc:mysql://localhost/spring"/>        < Propertyname= "User"value= "root"/>        < Propertyname= "Password"value= "System"/>        < Propertyname= "Maxpoolsize"value= "Max"/>        < Propertyname= "Minpoolsize"value= "2"/>        < Propertyname= "Initialpoolsize"value= "2"/>        < Propertyname= "MaxIdleTime"value= "+"/>    </Bean>        <!--the sessionfactory,sessionfactory that defines hibernate needs to rely on the data source and inject datasource -    <BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean">        < Propertyname= "DataSource"ref= "DataSource"/>        <!--annotatedclasses used to list all persisted classes -        < Propertyname= "Annotatedclasses">            <List>                <!--The following is used to list all PO classes -                <value>Edu.pri.lime.springhibernate.bean.Book</value>            </List>        </ Property>        <!--defining properties for Hibernate sessionfactory -        < Propertyname= "Hibernateproperties">            <Props>                <!--Specify the connection dialect for hibernate -                <propKey= "Hibernate.dialect">Org.hibernate.dialect.MySQL5InnoDBDialect</prop>                <!--whether to create a data table from Hibernate mappings -                <propKey= "Hibernate.hbm2ddl.auto">Update</prop>            </Props>        </ Property>    </Bean></Beans>

Once the Sessionfactory Bean is configured in the Spring IOC container, it will be loaded with the launch of the application and can take full advantage of the capabilities of the IOC container to inject the sessionfactory bean into any bean, such as the DAO component. Once the DAO component obtains the Sessionfactory reference, it can complete the actual database access.

Spring also supports access to the container data source, and if you need to use a container data source, modify the data source bean to the following configuration:

    <!---    < id = "myDataSource" class  = "Org.springframework.jndi.JndiObjectFactoryBean">        <   name= "Jndiname"  value= "java:comp/env/jdbc/myds"/>     </bean>

La La la

La La la

8--deep use of spring--8...2 manage Hibernate sessionfactory

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.