12 4月 2010

Spring3與JPA2 (1)

ORM的選擇很多,雖然JEE訂出了一個規格JPA,但之前並不喜歡使用,主要是因為相較其他原生的library像Hibernate, Eclipse Link等,JPA在實際開發上並不如其他Library來得方便,沒有像Hibernate的Criteria這類API就令我很感冒,所以在ORM的Demo裡一直沒有把JPA納入,多半使用的都還是Hibernate。
直到JPA2規格訂完,Hibernate也出了Stable的版本,想想也該納入看一看了。Hibernate與JPA所訂的EntityManager差異還不小,從中轉換還是要看一下JPA2的Spec,但我發現花較多時間的是在Spring的Configuration,要設定的部份較原來的純Hibernate要多出不少,而且有些我看了還是有些疑惑,看來單單有關Configuration就要寫上幾篇筆記了。
下列是基本的Spring Configurations:
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:util = "http://www.springframework.org/schema/util"  xmlns:p = "http://www.springframework.org/schema/p" 
   xmlns:tx = "http://www.springframework.org/schema/tx"  xmlns:aop = "http://www.springframework.org/schema/aop" 
   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/tx   http://www.springframework.org/schema/tx/spring-tx.xsd 
       http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop.xsd 
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">  
  
   < context:property-placeholder   location = "classpath:system.properties"  />  
  
   < bean   id = "dataSource"  class = "com.mchange.v2.c3p0.ComboPooledDataSource">  
    < property   name = "driverClass"  value = "${jdbc.driverClass}"  />  
    < property   name = "jdbcUrl"  value = "${jdbc.url}"  />  
    < property   name = "user"  value = "${jdbc.user}"  />  
    < property   name = "password"  value = "${jdbc.password}"  />  
    < property   name = "autoCommitOnClose"  value = "${jdbc.autoCommitOnClose}"  />  
   </ bean>  
   
   < bean   id = "entityManagerFactory" 
    class = "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
    < property   name = "persistenceXmlLocation"  value = "org/elliot/dao/jpaimpl/persistence.xml"  />  
    < property   name = "dataSource"  ref = "dataSource"  />  
    < property   name = "jpaVendorAdapter">  
     < bean   class = "org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
      < property   name = "database"  value = "${hibernate.database}"  />  
      < property   name = "showSql"  value = "${hibernate.show_sql}"  />  
      < property   name = "generateDdl"  value = "${hibernate.generate_ddl}"  />  
     </ bean>  
    </ property>  
    < property   name = "jpaPropertyMap">  
     < props>  
     < prop   key ="hibernate.cache.provider_class" > org.hibernate.cache.HashtableCacheProvider</ prop >  
     </ props>  
    </ property>  
   </ bean>  
  
   < bean   id = "entityManager" 
    class = "org.springframework.orm.jpa.support.SharedEntityManagerBean">  
    < property   name = "entityManagerFactory"  ref = "entityManagerFactory"  />  
   </ bean>  
  
   < bean   id = "transactionManager"  class = "org.springframework.orm.jpa.JpaTransactionManager">  
    < property   name = "entityManagerFactory"  ref = "entityManagerFactory"  />  
    < property   name = "dataSource"  ref = "dataSource"  />  
   </ bean>  
  
   < tx:annotation-driven  transaction-manager = "transactionManager"  
    proxy-target-class= "true"   />  
  
   < context:annotation-config  />  
   < context:component-scan   base-package = "org.elliot.dao.jpaimpl,org.elliot.service"  />  
  
</ beans> 

persistence.xml

< persistence   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" 
   version= "2.0" >  
   
   < persistence-unit   name = "Master"  transaction-type = "RESOURCE_LOCAL">  
   < class > org.elliot.domain.Master</ class >  
    < exclude-unlisted-classes/>  
   </ persistence-unit>  
  
</ persistence>

system.properties:

#h2database configurations
jdbc.driverClass= org.h2.Driver  
jdbc.url = jdbc:h2:~/test 
#jdbc.url=jdbc:h2:tcp://localhost/~/test 
jdbc.user= sa  
jdbc.password=  
jdbc.autoCommitOnClose= true  
  
hibernate.database= H2  
hibernate.dialect = org.hibernate.dialect.H2Dialect 
hibernate.show_sql= true  
hibernate.format_sql= true  
hibernate.generate_statistics= true  
hibernate.hbm2ddl.auto= create  
hibernate.cache.use_second_level_cache= true  
hibernate.generate_ddl= true

其中要注意的是Spring: entityManagerFactory的persistenceXmlLocation,基本上應是要指到META-INF下,這裡只是方便測試才搬出來,如果希望在其他不用Spring的案子使用要記得放回去,至於其他Spring還有許多相關的Configuration如loadTimeWeaver之類的就還在研究了。。。

沒有留言: