一 前言    最近在做个项目,JPA使用了hibernate3.6.后来觉得JPA单独使用不是很给力,于是就想到了和spring集成.刚开始,我是用spring2.5.6和hibernate3.6集成的,屡次不成功.后来研究发现,hibernate3.6的JPA是2.0的.狂晕.下面是spring3.0.5(支持JPA2.0)和hibernate3.6集成,其中源代码在附件里, 只有spring3.0.5的官方标准包,需要大家自己下载了,然后天家到WEB-INF/lib下,因为附件大小限制在了10M,所以除spring3.0.5官方包外其他所有的包以及数据库的SQL语句,都在本例中,若有需要,可以留言.

二 实验环境及公共环境配置
1.准备工具
eclipse3.6.1_javaee
jdk1.6.0.23
tomcat6.0.30
mysql5.1.54


1)mysql
mysql-connector-java-5.1.14-bin.jar

2)log4j
log4j-1.2.16.jar

3)spring3.0.5
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.aspects-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.instrument-3.0.5.RELEASE.jar
org.springframework.instrument.tomcat-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.jms-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.oxm-3.0.5.RELEASE.jar
org.springframework.spring-library-3.0.5.RELEASE.libd
org.springframework.test-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.portlet-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.web.struts-3.0.5.RELEASE.jar
自己下载的spring相关包,AOP时候用的
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.2.jar
commons-logging-1.1.1.jar

4)hibernate
antlr-2.7.6.jar
commons-collections-3.2.1.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar

2.数据源我们要先设置个tomcat数据源
文件位置:WebContent/META-INF/context.xml
Xml代码
<?xml version="1.0" encoding="UTF-8"?> 
<Context> 
<Resource   
    name="jdbc/test"   
    type="javax.sql.DataSource"   
    username="root"   
    password="147258369"   
    driverClassName="com.mysql.jdbc.Driver"   
    url="jdbc:mysql://127.0.0.l:3306/test?autoReconnect=true"   
    initialSize="1"   
    maxActive="5"   
    minIdle="1"   
    maxIdle="5"   
    validationQuery="select count(*) from dual"   
    testOnBorrow="true"   
    testOnReturnw="true"   
    testWhileIdlew="true"   
    minEvictableIdleTimeMillis="1800000"   
    timeBetweenEvictionRunsMillis="300000"   
    maxWait="1000"   
    removeAbandoned="true" 
    removeAbandonedTimeout="180" 
/> 
</Context> 

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/test"
type="javax.sql.DataSource"
username="root"
password="147258369"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.l:3306/test?autoReconnect=true"
initialSize="1"
maxActive="5"
minIdle="1"
maxIdle="5"
validationQuery="select count(*) from dual"
testOnBorrow="true"
testOnReturnw="true"
testWhileIdlew="true"
minEvictableIdleTimeMillis="1800000"
timeBetweenEvictionRunsMillis="300000"
maxWait="1000"
removeAbandoned="true"
removeAbandonedTimeout="180"
/>
</Context>
3.log4j配置
文件位置:src/log4j.properties
log4j.rootLogger=DEBUG,appender1
# org.springframework包下面所有的日志输出的级别设为DEBUG
log4j.logger.org.springframework=INFO
# 控制台输出
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.appender1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n
# 立即输出
log4j.appender.appender1.immediateFlush=true

三 搭建hibernate3.6(JPA)环境
利用MySQL5.1.54的test库,前提是test库的编码要是UTF8
test有3个字段
id,name,age

src/META-INF/persistence.xml
Xml代码
<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
    <persistence-unit name="sh" transaction-type="RESOURCE_LOCAL"> 
        <provider>org.hibernate.ejb.HibernatePersistence</provider> 
        <non-jta-data-source>java:comp/env/jdbc/test</non-jta-data-source> 
        <class>com.phl.entity.User</class> 
        <properties> 
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
            <property name="hibernate.connection.autocommit" value="true" /> 
        </properties> 
    </persistence-unit> 
</persistence> 

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="sh" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:comp/env/jdbc/test</non-jta-data-source>
<class>com.phl.entity.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.autocommit" value="true" />
</properties>
</persistence-unit>
</persistence>
四 搭建spring3.0.5环境
1.web.xml
Xml代码
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>sh</display-name> 
    <welcome-file-list> 
        <welcome-file>index.html</welcome-file> 
        <welcome-file>index.htm</welcome-file> 
        <welcome-file>index.jsp</welcome-file> 
        <welcome-file>default.html</welcome-file> 
        <welcome-file>default.htm</welcome-file> 
        <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <!-- log4j 必须要在最前面 --> 
    <context-param> 
        <param-name>log4jConfigLocation</param-name> 
        <param-value>classpath:log4j.properties</param-value> 
    </context-param> 
    <context-param> 
        <param-name>log4jRefreshInterval</param-name> 
        <param-value>60000</param-value> 
    </context-param> 
    <listener> 
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 
    <context-param> 
        <param-name>webAppRootKey</param-name> 
        <param-value>webApp.root</param-value> 
    </context-param> 
    <!-- Spring初始化 --> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app> 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>sh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- log4j 必须要在最前面 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<!-- Spring初始化 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

2.applicationContext.xml
Xml代码
<?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: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-3.0.xsd   
http://www.springframework.org/schema/context   
http://www.springframework.org/schema/context/spring-context-3.0.xsd   
http://www.springframework.org/schema/tx   
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
http://www.springframework.org/schema/aop    
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
    <bean id="user" class="com.phl.entity.manager.UserImpl" /> 
</beans> 

<?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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="user" class="com.phl.entity.manager.UserImpl" />
</beans>
五 搭建hibernate3.6与spring3.0.5集成环境
1.applicationContext.xml
Xml代码
<?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: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-3.0.xsd   
http://www.springframework.org/schema/context   
http://www.springframework.org/schema/context/spring-context-3.0.xsd   
http://www.springframework.org/schema/tx   
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
http://www.springframework.org/schema/aop    
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
    <bean id="user" class="com.phl.entity.manager.UserImpl" /> 
    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
        <!-- 此种方式不推荐,对jpa的侵入性太强,除非数据库连接池也在spring中配置 <property name="dataSource" ref="dataSource" /> --> 
        <property name="persistenceUnitName" value="sh" /> 
    </bean> 
    <!-- 自动装载EntityManager --> 
    <context:annotation-config /> 
    <!-- 配置事务管理器 --> 
    <bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
        <property name="entityManagerFactory" ref="myEmf" /> 
    </bean> 
    <!-- 配置切面 --> 
    <aop:config> 
        <aop:pointcut expression="execution(* com.phl.entity.manager.*.*(..))" id="myTx1"/> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myTx1"/> 
    </aop:config> 
    <!-- 配制具体方法及事务参数 --> 
    <tx:advice id="txAdvice" transaction-manager="myTxManager"> 
        <tx:attributes> 
            <tx:method name="*" propagation="REQUIRED" /> 
        </tx:attributes> 
    </tx:advice> 
<!--   
事务传播行为类型  
 
REQUIRED  
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。  
   
SUPPORTS  
支持当前事务,如果当前没有事务,就以非事务方式执行。  
   
MANDATORY  
使用当前的事务,如果当前没有事务,就抛出异常。  
   
REQUIRES_NEW  
新建事务,如果当前存在事务,把当前事务挂起。  
   
NOT_SUPPORTED  
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。  
   
NEVER  
以非事务方式执行,如果当前存在事务,则抛出异常。  
   
NESTED  
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。  
--> 
</beans> 

<?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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="user" class="com.phl.entity.manager.UserImpl" />
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 此种方式不推荐,对jpa的侵入性太强,除非数据库连接池也在spring中配置 <property name="dataSource" ref="dataSource" /> -->
<property name="persistenceUnitName" value="sh" />
</bean>
<!-- 自动装载EntityManager -->
<context:annotation-config />
<!-- 配置事务管理器 -->
<bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.phl.entity.manager.*.*(..))" id="myTx1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myTx1"/>
</aop:config>
<!-- 配制具体方法及事务参数 -->
<tx:advice id="txAdvice" transaction-manager="myTxManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!--
事务传播行为类型

REQUIRED
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

SUPPORTS
支持当前事务,如果当前没有事务,就以非事务方式执行。

MANDATORY
使用当前的事务,如果当前没有事务,就抛出异常。

REQUIRES_NEW
新建事务,如果当前存在事务,把当前事务挂起。

NOT_SUPPORTED
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

NEVER
以非事务方式执行,如果当前存在事务,则抛出异常。

NESTED
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。
-->
</beans>
六 总结
集成的过程看着很乱,其实很简单,初始化环境是首先要配置好log4j和tomcat数据源,如果没有特殊要求,我们一般配在工程下面context,而不是tomcat的全局数据源
1。首先可以独立使用hibernate3.6
2。可以独立使用spring3.0.5
3。可以由spring管理hibernate3.6的EntityManager:好处是不用关闭EntityManager
4。可以由spring来管理事务:好处是不用每提提交事务,由spring帮助完成提交
最后祝大家开发愉快,如果有任何问题,都可以留言,我会第一时间给大家解答.本文也并不是什么高端的文章,只是给自己当个笔记记录下来.如有不当之处,请各位同仁理解.

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐