spring3.0.5与hibernate3.6(JPA)的基成详细说明

一 前言     最近在做个项目,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代码 复制代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < Context >   
  3. < Resource     
  4.      name = "jdbc/test"     
  5.      type = "javax.sql.DataSource"     
  6.      username = "root"     
  7.      password = "147258369"     
  8.      driverClassName = "com.mysql.jdbc.Driver"     
  9.      url = "jdbc:mysql://127.0.0.l:3306/test?autoReconnect=true"     
  10.      initialSize = "1"     
  11.      maxActive = "5"     
  12.      minIdle = "1"     
  13.      maxIdle = "5"     
  14.      validationQuery = "select count(*) from dual"     
  15.      testOnBorrow = "true"     
  16.      testOnReturnw = "true"     
  17.      testWhileIdlew = "true"     
  18.      minEvictableIdleTimeMillis = "1800000"     
  19.      timeBetweenEvictionRunsMillis = "300000"     
  20.      maxWait = "1000"     
  21.      removeAbandoned = "true"   
  22.      removeAbandonedTimeout = "180"   
  23. />   
  24. </ 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代码 复制代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < 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" >   
  3.      < persistence-unit   name = "sh"   transaction-type = "RESOURCE_LOCAL" >   
  4.          < provider > org.hibernate.ejb.HibernatePersistence </ provider >   
  5.          < non-jta-data-source > java:comp/env/jdbc/test </ non-jta-data-source >   
  6.          < class > com.phl.entity.User </ class >   
  7.          < properties >   
  8.              < property   name = "hibernate.dialect"   value = "org.hibernate.dialect.MySQLDialect"   />   
  9.              < property   name = "hibernate.connection.autocommit"   value = "true"   />   
  10.          </ properties >   
  11.      </ persistence-unit >   
  12. </ 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代码 复制代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < 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" >   
  3.      < display-name > sh </ display-name >   
  4.      < welcome-file-list >   
  5.          < welcome-file > index.html </ welcome-file >   
  6.          < welcome-file > index.htm </ welcome-file >   
  7.          < welcome-file > index.jsp </ welcome-file >   
  8.          < welcome-file > default.html </ welcome-file >   
  9.          < welcome-file > default.htm </ welcome-file >   
  10.          < welcome-file > default.jsp </ welcome-file >   
  11.      </ welcome-file-list >   
  12.      <!-- log4j 必须要在最前面 -->   
  13.      < context-param >   
  14.          < param-name > log4jConfigLocation </ param-name >   
  15.          < param-value > classpath:log4j.properties </ param-value >   
  16.      </ context-param >   
  17.      < context-param >   
  18.          < param-name > log4jRefreshInterval </ param-name >   
  19.          < param-value > 60000 </ param-value >   
  20.      </ context-param >   
  21.      < listener >   
  22.          < listener-class > org.springframework.web.util.Log4jConfigListener </ listener-class >   
  23.      </ listener >   
  24.      < context-param >   
  25.          < param-name > webAppRootKey </ param-name >   
  26.          < param-value > webApp.root </ param-value >   
  27.      </ context-param >   
  28.      <!-- Spring初始化 -->   
  29.      < context-param >   
  30.          < param-name > contextConfigLocation </ param-name >   
  31.          < param-value > /WEB-INF/applicationContext.xml </ param-value >   
  32.      </ context-param >   
  33.      < listener >   
  34.          < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >   
  35.      </ listener >   
  36. </ 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代码 复制代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < 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"   
  3.      xsi:schemaLocation ="http://www.springframework.org/schema/beans    
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  5. http://www.springframework.org/schema/context    
  6. http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  7. http://www.springframework.org/schema/tx    
  8. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  9. http://www.springframework.org/schema/aop     
  10. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >   
  11.      < bean   id = "user"   class = "com.phl.entity.manager.UserImpl"   />   
  12. </ 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代码 复制代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < 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"   
  3.      xsi:schemaLocation ="http://www.springframework.org/schema/beans    
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  5. http://www.springframework.org/schema/context    
  6. http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  7. http://www.springframework.org/schema/tx    
  8. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  9. http://www.springframework.org/schema/aop     
  10. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >   
  11.      < bean   id = "user"   class = "com.phl.entity.manager.UserImpl"   />   
  12.      < bean   id = "myEmf"   class = "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >   
  13.          <!-- 此种方式不推荐,对jpa的侵入性太强,除非数据库连接池也在spring中配置 <property name="dataSource" ref="dataSource" /> -->   
  14.          < property   name = "persistenceUnitName"   value = "sh"   />   
  15.      </ bean >   
  16.      <!-- 自动装载EntityManager -->   
  17.      < context:annotation-config   />   
  18.      <!-- 配置事务管理器 -->   
  19.      < bean   id = "myTxManager"   class = "org.springframework.orm.jpa.JpaTransactionManager" >   
  20.          < property   name = "entityManagerFactory"   ref = "myEmf"   />   
  21.      </ bean >   
  22.      <!-- 配置切面 -->   
  23.      < aop:config >   
  24.          < aop:pointcut   expression = "execution(* com.phl.entity.manager.*.*(..))"   id = "myTx1" />   
  25.          < aop:advisor   advice-ref = "txAdvice"   pointcut-ref = "myTx1" />   
  26.      </ aop:config >   
  27.      <!-- 配制具体方法及事务参数 -->   
  28.      < tx:advice   id = "txAdvice"   transaction-manager = "myTxManager" >   
  29.          < tx:attributes >   
  30.              < tx:method   name = "*"   propagation = "REQUIRED"   />   
  31.          </ tx:attributes >   
  32.      </ tx:advice >   
  33. <!--    
  34. 事务传播行为类型   
  35.   
  36. REQUIRED   
  37.  如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。   
  38.     
  39. SUPPORTS   
  40.  支持当前事务,如果当前没有事务,就以非事务方式执行。   
  41.     
  42. MANDATORY   
  43.  使用当前的事务,如果当前没有事务,就抛出异常。   
  44.     
  45. REQUIRES_NEW   
  46.  新建事务,如果当前存在事务,把当前事务挂起。   
  47.     
  48. NOT_SUPPORTED   
  49.  以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。   
  50.     
  51. NEVER   
  52.  以非事务方式执行,如果当前存在事务,则抛出异常。   
  53.     
  54. NESTED   
  55.  如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。   
  56. -- >   
  57. </ 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帮助完成提交
最后祝大家开发愉快,如果有任何问题,都可以留言,我会第一时间给大家解答.本文也并不是什么高端的文章,只是给自己当个笔记记录下来.如有不当之处,请各位同仁理解.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值