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

          下面帖的是在SE的環(huán)境下面使用toplink和hibernate的實(shí)現(xiàn),兩者都寫在同一個(gè)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的環(huán)境下面,是不能使用容器的JTA的數(shù)據(jù)源的。并且不能使用
          <exclude-unlisted-classes>true</exclude-unlisted-classes>這個(gè)屬性。
          本文重點(diǎn)是記錄下兩個(gè)常用的JPA的實(shí)現(xiàn)的配置。目前是在SE環(huán)境下的配置。EE環(huán)境下面的配置如下:
          <?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環(huán)境下面使用JPA配置就簡(jiǎn)單了許多,首先他可以把當(dāng)前模塊的CLASS文件都包括進(jìn)來,不用手工指定。并且也少了很多有關(guān)于數(shù)據(jù)庫連接的操作,因?yàn)檫@個(gè)時(shí)候都是從容器里面去取數(shù)據(jù)源的。并且此時(shí)的事務(wù)是由容器去管理的,也就是使用JTA,不再是RESOURCE_LOCAL了。這樣在代碼里面就不用em.getTransaction().begin();和em.getTransaction().commit()了,并且可以使用注入功能,把EntityManager注入到使用它的地方了。






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

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

          FeedBack:
          # re: 使用JPA的不同實(shí)現(xiàn)的配置[未登錄]
          2008-10-19 22:56 | ytl
          好久沒看到你寫文章啦。。  回復(fù)  更多評(píng)論
            
          # re: 使用JPA的不同實(shí)現(xiàn)的配置
          2008-10-21 09:40 | attend
          多謝分享.  回復(fù)  更多評(píng)論
            
          # re: 使用JPA的不同實(shí)現(xiàn)的配置[未登錄]
          2009-09-22 01:14 | CC
          除了persistence.xml還要配置其它的地方嗎?
          想在tomcat中使用它  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 个旧市| 庆安县| 高安市| 长宁县| 越西县| 阿合奇县| 增城市| 科技| 奈曼旗| 博兴县| 上蔡县| 梁平县| 进贤县| 新昌县| 寻乌县| 双鸭山市| 大丰市| 大城县| 永清县| 雷州市| 射阳县| 千阳县| 罗田县| 航空| 广安市| 延庆县| 连州市| 宜兴市| 台山市| 曲麻莱县| 新干县| 汝阳县| 恩平市| 合作市| 巨鹿县| 鄂托克旗| 定边县| 扎囊县| 五峰| 广西| 板桥市|