千里冰封
          JAVA 濃香四溢
          posts - 151,comments - 2801,trackbacks - 0
          以前使用JPA的實現是toplink,現在改為hibernate,所以要修改persistence.xml文件,兩者的配置有一些不一樣,并且在EE環境下面和SE的環境下面也有不一樣,還有一點,那就是當persistence.xml里面有些格式出錯的時候,雖然出錯的不是我們需要的那個單元,但也會使得整個persistence.xml報廢。

          下面帖的是在SE的環境下面使用toplink和hibernate的實現,兩者都寫在同一個persistence.xml里面。這樣切換起來也方便一些。

          <?xml version="1.0" encoding="UTF-8"?>
          <persistence version="1.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_1_0.xsd">
            
          <persistence-unit name="TestSSH2PU" transaction-type="RESOURCE_LOCAL">
              
          <provider>oracle.toplink.essentials.PersistenceProvider</provider>
              
          <class>com.hadeslee.jpaentity.Department</class>
              
          <class>com.hadeslee.jpaentity.Person</class>
              
          <properties>
                
          <property name="toplink.jdbc.user" value="sa"/>
                
          <property name="toplink.jdbc.password" value="hadeslee"/>
                
          <property name="toplink.jdbc.url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testSSH"/>
                
          <property name="toplink.jdbc.driver" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
                
          <property name="toplink.ddl-generation" value="create-tables"/>
              
          </properties>
            
          </persistence-unit>
            
          <persistence-unit name="TestSSH1PU2" transaction-type="RESOURCE_LOCAL">
              
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
              
          <class>com.hadeslee.jpaentity.Department</class>
              
          <class>com.hadeslee.jpaentity.Person</class>
             <properties>
                  
          <property name="hibernate.connection.driver_class" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
                  
          <property name="hibernate.connection.url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testSSH"></property>
                  
          <property name="hibernate.connection.username" value="sa"></property>
                  
          <property name="hibernate.connection.password" value="hadeslee"></property>
                  
          <property name="hibernate.show_sql" value="true"></property>
                  
          <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"></property>
                  
          <property name="hibernate.current_session_context_class" value="thread"></property>
              
          </properties>
            
          </persistence-unit>
          </persistence>


          在SE的環境下面,是不能使用容器的JTA的數據源的。并且不能使用
          <exclude-unlisted-classes>true</exclude-unlisted-classes>這個屬性。
          本文重點是記錄下兩個常用的JPA的實現的配置。目前是在SE環境下的配置。EE環境下面的配置如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <persistence version="1.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_1_0.xsd">
            
          <persistence-unit name="unit_mssql" transaction-type="JTA">
              
          <provider>oracle.toplink.essentials.PersistenceProvider</provider>
              
          <jta-data-source>MobileOAMSSQL</jta-data-source>
              
          <properties>
                
          <property name="toplink.ddl-generation" value="create-tables"/>
                
          <property name="toplink.logging.level" value="FINE"/>
              
          </properties>
            
          </persistence-unit>
           
          <persistence-unit name="MyApp-ejbPU2" transaction-type="JTA">
              
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
              
          <jta-data-source>MobileOAMYSQL</jta-data-source>
              
          <properties>
                
          <property name="hibernate.hbm2ddl.auto" value="update"/>
                
          <property name="hibernate.show_sql" value="true"/>
              
          </properties>
            
          </persistence-unit>
          </persistence>

          在EE環境下面使用JPA配置就簡單了許多,首先他可以把當前模塊的CLASS文件都包括進來,不用手工指定。并且也少了很多有關于數據庫連接的操作,因為這個時候都是從容器里面去取數據源的。并且此時的事務是由容器去管理的,也就是使用JTA,不再是RESOURCE_LOCAL了。這樣在代碼里面就不用em.getTransaction().begin();和em.getTransaction().commit()了,并且可以使用注入功能,把EntityManager注入到使用它的地方了。






          盡管千里冰封
          依然擁有晴空

          你我共同品味JAVA的濃香.
          posted on 2008-10-19 18:38 千里冰封 閱讀(5055) 評論(3)  編輯  收藏 所屬分類: JAVAEE

          FeedBack:
          # re: 使用JPA的不同實現的配置[未登錄]
          2008-10-19 22:56 | ytl
          好久沒看到你寫文章啦。。  回復  更多評論
            
          # re: 使用JPA的不同實現的配置
          2008-10-21 09:40 | attend
          多謝分享.  回復  更多評論
            
          # re: 使用JPA的不同實現的配置[未登錄]
          2009-09-22 01:14 | CC
          除了persistence.xml還要配置其它的地方嗎?
          想在tomcat中使用它  回復  更多評論
            
          主站蜘蛛池模板: 盐山县| 迭部县| 山阴县| 宝兴县| 天全县| 宜阳县| 望江县| 石楼县| 兴安县| 康平县| 白城市| 桂东县| 建德市| 横山县| 安远县| 仁怀市| 邯郸市| 土默特左旗| 米脂县| 柘城县| 武隆县| 旬阳县| 蒙山县| 嘉鱼县| 定南县| 屯留县| 曲周县| 攀枝花市| 达日县| 元阳县| 临城县| 罗甸县| 尤溪县| 武义县| 威宁| 青铜峡市| 雅安市| 财经| 上栗县| 芮城县| 长泰县|