現(xiàn)在需要下載Hibernate。Hibernate的 站點(diǎn)整潔、內(nèi)容豐富,而且下載過程沒有一點(diǎn)兒延遲。兩個(gè)單獨(dú)的下載包括Hibernate Core version 3.2和一個(gè)稱為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類庫的信息?但重要的是我們需要哪個(gè)類庫?在Hibernate Core中,我發(fā)現(xiàn)了39個(gè)庫,而它的 JPA覆蓋有6個(gè)之多。我采用了省事的方式。在這個(gè)簡(jiǎn)單的 Ant任務(wù)的幫助下,將所有的45個(gè)庫放入了Weblogic Server實(shí)例域的共享庫(即${bea.home}/wlserver_10.0/samples/domains/wl_server/lib)中。
當(dāng)然,有些庫應(yīng)該更容易區(qū)分,應(yīng)該除去那些明顯的諸如jta.jar或junit-3.8.1.jar的庫,F(xiàn)在這些庫中許多是Weblogic Server 10.0集成的一部分。實(shí)際上,已經(jīng)在${bea.home}/modules目錄中發(fā)現(xiàn)了140個(gè)jar。要知道有很多關(guān)于Weblogic中的antlr庫與Hibernate中的antlr庫之間發(fā)生沖突的恐怖故事,不過我還未遇到過。
關(guān)于在共享空間放置Hibernate庫的重點(diǎn)是它們沒有與應(yīng)用程序封裝在一起。在相同的域中部署的任何應(yīng)用程序現(xiàn)在都可以使用Hibernate。當(dāng)有許多依賴于不同Hibernate版本的應(yīng)用程序時(shí),很明顯這種方式不再適用了。否則,在共享庫中的這種放置方式可以避免其他的問題(產(chǎn)生問題的bug還沒有投下陰影)。
部署和運(yùn)行
現(xiàn)在,已經(jīng)安裝了Hibernate,編寫了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的配置沒有指定事務(wù)類型。容器環(huán)境中默認(rèn)的事務(wù)類型是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)用程序沒有出錯(cuò)。但是測(cè)試沒有成功。
[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)
通過了一個(gè)測(cè)試。用org.hibernate.impl.SessionImpl,確切地說,是它的代理對(duì)這個(gè)bean進(jìn)行了注入。
但是,為什么向 Message實(shí)例分配標(biāo)識(shí)符沒有成功?
這只不過因?yàn)槲覀冊(cè)O(shè)計(jì)bean使用容器管理事務(wù),而現(xiàn)在卻配置為使用本地事務(wù)。所以我們的bean代碼和容器都沒有提交事務(wù),而且因?yàn)闃?biāo)識(shí)值指定為由提供者賦值(見Message.java中的@Id注釋)并且由Hibernate利用數(shù)據(jù)庫列的自動(dòng)增量特性為標(biāo)識(shí)賦值——所以也沒有提交表示沒有標(biāo)識(shí)值賦值。
當(dāng)然,我們能夠在JPAServiceBean.log()方法中添加明確的事務(wù)分界;但這不是解決方案。在容器環(huán)境中,我們傾向于使用容器管理的事務(wù),傾向于配置持久性單元使用JTA。
如何告知Hibernate使用Weblogic事務(wù)管理器并將它的事務(wù)與容器事務(wù)結(jié)合起來?我通過Google進(jìn)行了幾分鐘的搜索找到了答案。向persistence.xml添加以下屬性。