最近在做個項目,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.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-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)hibernate3.6
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 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包下面所有的日誌輸出的級別設爲INFO
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 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 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 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 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,獲得像在ejb3下面使用JPA一樣的快感
4。可以由spring來管理事務:好處是不用每提提交事務,由spring幫助完成提交
最後祝大家開發愉快,如果有任何問題,都可以留言,我會第一時間給大家解答.本文也並不是什麼高端的文章,只是給自己當個筆記記錄下來.如有不當之處,請各位同仁理解.