jpa+spring+hibernate

一 前言   
    最近在做個項目,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代碼  收藏代碼
  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>  

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代碼  收藏代碼
  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>  

四 搭建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>  


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>  

五 搭建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> 
六 總結
集成的過程看着很亂,其實很簡單,初始化環境是首先要配置好log4j和tomcat數據源,如果沒有特殊要求,我們一般配在工程下面context,而不是tomcat的全局數據源
1。首先可以獨立使用hibernate3.6
2。可以獨立使用spring3.0.5
3。可以由spring管理hibernate3.6的EntityManager:好處是不用關閉EntityManager,獲得像在ejb3下面使用JPA一樣的快感
4。可以由spring來管理事務:好處是不用每提提交事務,由spring幫助完成提交
    最後祝大家開發愉快,如果有任何問題,都可以留言,我會第一時間給大家解答.本文也並不是什麼高端的文章,只是給自己當個筆記記錄下來.如有不當之處,請各位同仁理解.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章