您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 >
插入式持久性提供者的承諾:Kodo、OpenJPA和Hibernate
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/2/27 14:47:53 ] 推薦標(biāo)簽:

現(xiàn)在需要下載Hibernate。Hibernate的 站點(diǎn)整潔、內(nèi)容豐富,而且下載過(guò)程沒(méi)有一點(diǎn)兒延遲。兩個(gè)單獨(dú)的下載包括Hibernate Core version 3.2和一個(gè)稱(chēng)為Hibernate EntityManager vers的JPA覆蓋。

<target name="install-hibernate">
    <property name="domain.lib" value="C:/bea/wlserver_10.0/samples/domains/wl_server/lib"/>
    <property name="hibernate.core.dir" value="D:/hibernate/core-3.2"/>
    <property name="hibernate.core.dir" value="D:/hibernate/entitymanager-3.3.1"/>
   
    <copy todir="${domain.lib}" overwrite="false">
        <fileset dir="${hibernate.core.dir}">
           <include name="hibernate*.jar"/>
        </fileset>
        <fileset dir="${hibernate.core.dir}/lib">
           <include name="*.jar"/>
        </fileset>
        <fileset dir="${hibernate.jpa.dir}">
           <include name="hibernate*.jar"/>
        </fileset>
        <fileset dir="${hibernate.jpa.dir}/lib">
           <include name="*.jar"/>
        </fileset>
    </copy>
</target>

    如何告知Weblogic Server關(guān)于Hibernate類(lèi)庫(kù)的信息?但重要的是我們需要哪個(gè)類(lèi)庫(kù)?在Hibernate Core中,我發(fā)現(xiàn)了39個(gè)庫(kù),而它的 JPA覆蓋有6個(gè)之多。我采用了省事的方式。在這個(gè)簡(jiǎn)單的 Ant任務(wù)的幫助下,將所有的45個(gè)庫(kù)放入了Weblogic Server實(shí)例域的共享庫(kù)(即${bea.home}/wlserver_10.0/samples/domains/wl_server/lib)中。

當(dāng)然,有些庫(kù)應(yīng)該更容易區(qū)分,應(yīng)該除去那些明顯的諸如jta.jar或junit-3.8.1.jar的庫(kù),F(xiàn)在這些庫(kù)中許多是Weblogic Server 10.0集成的一部分。實(shí)際上,已經(jīng)在${bea.home}/modules目錄中發(fā)現(xiàn)了140個(gè)jar。要知道有很多關(guān)于Weblogic中的antlr庫(kù)與Hibernate中的antlr庫(kù)之間發(fā)生沖突的恐怖故事,不過(guò)我還未遇到過(guò)。

關(guān)于在共享空間放置Hibernate庫(kù)的重點(diǎn)是它們沒(méi)有與應(yīng)用程序封裝在一起。在相同的域中部署的任何應(yīng)用程序現(xiàn)在都可以使用Hibernate。當(dāng)有許多依賴于不同Hibernate版本的應(yīng)用程序時(shí),很明顯這種方式不再適用了。否則,在共享庫(kù)中的這種放置方式可以避免其他的問(wèn)題(產(chǎn)生問(wèn)題的bug還沒(méi)有投下陰影)。
部署和運(yùn)行

現(xiàn)在,已經(jīng)安裝了Hibernate,編寫(xiě)了Java源,配置文件和構(gòu)建腳本也已經(jīng)緒。下一步要對(duì)應(yīng)用程序進(jìn)行編譯、封裝和部署。我正是這樣做的。

$ ant -Dprovider=hibernate deploy

在服務(wù)器端,我得到一個(gè)堆棧跟蹤:(

weblogic.application.ModuleException: Exception preparing module: EJBModule(hibernate-ejb.jar)

        at weblogic.ejb.container.deployer.EJBModule.prepare(EJBModule.java:399)
        at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:93)
        at weblogic.application.internal.flow.DeploymentCallbackFlow$1.next(DeploymentCallbackFlow.java:360)
        at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:26)
        at weblogic.application.internal.flow.DeploymentCallbackFlow.prepare(DeploymentCallbackFlow.java:56)
        Truncated. see log file for complete stacktrace
                        
org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
        at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:329)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:730)
        at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:127)
                     

這個(gè)錯(cuò)誤消息是有意義的。persistence.xml的配置沒(méi)有指定事務(wù)類(lèi)型。容器環(huán)境中默認(rèn)的事務(wù)類(lèi)型是JTA。當(dāng)未將它被告知Weblogic Server的事務(wù)管理器的信息時(shí),Hibernate會(huì)有所抱怨。配置Hibernate使之明確使用本地事務(wù)怎么樣?我編輯了persistence.xml

<persistence-unit name="test" 
                       
transaction-type="RESOURCE_LOCAL">
                     

然后再次運(yùn)行構(gòu)建腳本,它將部署并運(yùn)行JUnit測(cè)試,

$ ant -Dprovider=hibernate

讓我驚喜的是,這次部署的應(yīng)用程序沒(méi)有出錯(cuò)。但是測(cè)試沒(méi)有成功。

   [echo] Running JUnit Test: junit.TestJPAService ...
   [junit] Logical Persistence Provider is [hibernate]
   [junit] Actual  Persistence Provider is [org.hibernate.impl.SessionImpl]
   [junit] Test junit.TestJPAService FAILED
   [echo] *** ERROR: There are test failures. Output is shown below
   [concat] Testsuite: junit.TestJPAService
   [concat] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 2.934 sec
   [concat]
   [concat] ------------- Standard Output ---------------
   [concat] Contacting server t3://localhost:7001 as weblogic for JPAService
   [concat] ------------- ---------------- ---------------
   [concat] ------------- Standard Error -----------------
   [concat] Logical Persistence Provider is [hibernate]
   [concat] Actual  Persistence Provider is [org.hibernate.impl.SessionImpl]
   [concat] ------------- ---------------- ---------------
   [concat] Testcase: testLog(junit.TestJPAService):    FAILED
   [concat] Message is not assigned any identifier
   [concat] junit.framework.AssertionFailedError: Message is not assigned any identifier
   [concat]     at junit.TestJPAService.testLog(Unknown Source)

通過(guò)了一個(gè)測(cè)試。用org.hibernate.impl.SessionImpl,確切地說(shuō),是它的代理對(duì)這個(gè)bean進(jìn)行了注入。

但是,為什么向 Message實(shí)例分配標(biāo)識(shí)符沒(méi)有成功?

這只不過(guò)因?yàn)槲覀冊(cè)O(shè)計(jì)bean使用容器管理事務(wù),而現(xiàn)在卻配置為使用本地事務(wù)。所以我們的bean代碼和容器都沒(méi)有提交事務(wù),而且因?yàn)闃?biāo)識(shí)值指定為由提供者賦值(見(jiàn)Message.java中的@Id注釋?zhuān)┎⑶矣蒆ibernate利用數(shù)據(jù)庫(kù)列的自動(dòng)增量特性為標(biāo)識(shí)賦值——所以也沒(méi)有提交表示沒(méi)有標(biāo)識(shí)值賦值。

當(dāng)然,我們能夠在JPAServiceBean.log()方法中添加明確的事務(wù)分界;但這不是解決方案。在容器環(huán)境中,我們傾向于使用容器管理的事務(wù),傾向于配置持久性單元使用JTA。

如何告知Hibernate使用Weblogic事務(wù)管理器并將它的事務(wù)與容器事務(wù)結(jié)合起來(lái)?我通過(guò)Google進(jìn)行了幾分鐘的搜索找到了答案。向persistence.xml添加以下屬性。

上一頁(yè)123456下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd