Hibernate and Spring Integration

Source: Internet
Author: User
Tags aop

Spring and Hibernate integrate key points:

1) Hibernate 's sessionfactory object is given to Spring to create;

2) hibernate transaction is given to spring 's declarative transaction management.

1. Deptdao.java

Data Access Layer

Public class Deptdao {

Spring and Hibernate Integration: IOC Container injection

Private sessionfactory sessionfactory;

Public void setsessionfactory (sessionfactory sessionfactory) {

this. sessionfactory = Sessionfactory;

}

Save a record

Spring and Hibernate Integration: Transaction management given to spring

Public void Save (Dept Dept) {

Sessionfactory.getcurrentsession (). Save (dept);

}

}

2. Deptservice

Public class Deptservice {

Private Deptdao Deptdao;

Public void Setdeptdao (Deptdao Deptdao) {

this. Deptdao = Deptdao;

}

Public void Save (Dept Dept) {

Deptdao.save (dept);

}

}

3. App.java Test

Public class App {

Container

Private ApplicationContext AC = new classpathxmlapplicationcontext ("Bean.xml");

@Test

Public void testApp () throws Exception {

Deptservice Deptservie = (deptservice) ac.getbean ("Deptservice");

System. out. println (Deptservie.getclass ());

Deptservie.save (new Dept ());

}

}

4. Bean.xml Configuration "Spring management sessionfactory in 3 Ways"

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="Http://www.springframework.org/schema/beans"

xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="Http://www.springframework.org/schema/context"

xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"

xmlns:tx="Http://www.springframework.org/schema/tx"

xsi:schemalocation="Http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context.xsd

Http://www.springframework.org/schema/aop

Http://www.springframework.org/schema/aop/spring-aop.xsd

Http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd ">

<!-- DAO Instance --

<bean id="Deptdao" class="Cn.itcast.dao.DeptDao">

<property name="sessionfactory" ref="Sessionfactory"></property>

</bean>

<!--service instance --

<bean id="Deptservice" class="Cn.itcast.service.DeptService">

<property name="Deptdao" ref="Deptdao"></property>

</bean>

<!-- Data source configuration --

<bean id="DataSource" class="Com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="Driverclass" value="Com.mysql.jdbc.Driver"></property>

<property name="Jdbcurl" value="Jdbc:mysql:///hib_demo"></property>

<property name="user" value="root"></property>

<property name="Password " value="root"></property>

<property name= "initialpoolsize " value="3"></property>

<property name="maxpoolsize " value="ten"></property>

<property name="maxstatements " value="></property>"

<property name= "acquireincrement " value="2"></property>

</bean>

<!--########## #Spring and Hibernate integration start########### --

<!-- Way (1) Direct loading of hibernate.cfg.xml files in a way that integrates

<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" ></property>

</bean>

<!-- Way (2) connect pool to Spring Management "Part of the configuration is written to hibernate , one in In spring , complete the

<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" ></property>

<property name= "DataSource" ref= "DataSource" ></property>

</bean>

<!-- "recommended" Way (3) All configurations are done in the Spring configuration file -

<bean id="sessionfactory" class="Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

<!-- inject connection pool objects --

<property name="DataSource" ref="DataSource"></property>

<!-- hibernate Common configuration -

<property name="Hibernateproperties">

<props>

<prop key="Hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<prop key="Hibernate.show_sql">true</prop>

<prop key="Hibernate.hbm2ddl.auto">update</prop>

</props>

</property>

<!-- Hibernate Mapping configuration

<property name= "Mappinglocations" >

<list>

<value>classpath:cn/itcast/entity/*.hbm.xml</value>

</list>

</property>

-

<property name="Mappingdirectorylocations">

<list>

<value>classpath:cn/itcast/entity/</value>

</list>

</property>

</bean>

<!--########## #Spring and Hibernate integration end########### --

<!-- transaction configuration --

<!--A. Configure the transaction manager class --

<bean id="Txmanager" class="Org.springframework.orm.hibernate3.HibernateTransactionManager " >

<property name="sessionfactory" ref="Sessionfactory"></property>

</bean>

<!--b. configuring transaction Enhancement ( after blocking to a method if management transaction ?) --

<tx:advice id="Txadvice" transaction-manager="Txmanager">

<tx:attributes>

<tx:method name="*" read-only="false"/>

</tx:attributes>

</tx:advice>

<!--c. Aop Configuration --

<aop:config>

<aop:pointcut expression="Execution (* cn.itcast.service.*.* (..))" id="pt"/>

<aop:advisor advice-ref="Txadvice" pointcut-ref="pt"/>

</aop:config>

</beans>

Hibernate and Spring Integration

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.