現在需要下載Hibernate。Hibernate的 站點整潔、內容豐富,而且下載過程沒有一點兒延遲。兩個單獨的下載包括Hibernate Core version 3.2和一個稱為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關于Hibernate類庫的信息?但重要的是我們需要哪個類庫?在Hibernate Core中,我發現了39個庫,而它的 JPA覆蓋有6個之多。我采用了省事的方式。在這個簡單的 Ant任務的幫助下,將所有的45個庫放入了Weblogic Server實例域的共享庫(即${bea.home}/wlserver_10.0/samples/domains/wl_server/lib)中。
當然,有些庫應該更容易區分,應該除去那些明顯的諸如jta.jar或junit-3.8.1.jar的庫。現在這些庫中許多是Weblogic Server 10.0集成的一部分。實際上,已經在${bea.home}/modules目錄中發現了140個jar。要知道有很多關于Weblogic中的antlr庫與Hibernate中的antlr庫之間發生沖突的恐怖故事,不過我還未遇到過。
關于在共享空間放置Hibernate庫的重點是它們沒有與應用程序封裝在一起。在相同的域中部署的任何應用程序現在都可以使用Hibernate。當有許多依賴于不同Hibernate版本的應用程序時,很明顯這種方式不再適用了。否則,在共享庫中的這種放置方式可以避免其他的問題(產生問題的bug還沒有投下陰影)。
部署和運行
現在,已經安裝了Hibernate,編寫了Java源,配置文件和構建腳本也已經緒。下一步要對應用程序進行編譯、封裝和部署。我正是這樣做的。
$ ant -Dprovider=hibernate deploy
在服務器端,我得到一個堆棧跟蹤:(
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)
這個錯誤消息是有意義的。persistence.xml的配置沒有指定事務類型。容器環境中默認的事務類型是JTA。當未將它被告知Weblogic Server的事務管理器的信息時,Hibernate會有所抱怨。配置Hibernate使之明確使用本地事務怎么樣?我編輯了persistence.xml
<persistence-unit name="test"
transaction-type="RESOURCE_LOCAL">
然后再次運行構建腳本,它將部署并運行JUnit測試,
$ ant -Dprovider=hibernate
讓我驚喜的是,這次部署的應用程序沒有出錯。但是測試沒有成功。
[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)
通過了一個測試。用org.hibernate.impl.SessionImpl,確切地說,是它的代理對這個bean進行了注入。
但是,為什么向 Message實例分配標識符沒有成功?
這只不過因為我們設計bean使用容器管理事務,而現在卻配置為使用本地事務。所以我們的bean代碼和容器都沒有提交事務,而且因為標識值指定為由提供者賦值(見Message.java中的@Id注釋)并且由Hibernate利用數據庫列的自動增量特性為標識賦值——所以也沒有提交表示沒有標識值賦值。
當然,我們能夠在JPAServiceBean.log()方法中添加明確的事務分界;但這不是解決方案。在容器環境中,我們傾向于使用容器管理的事務,傾向于配置持久性單元使用JTA。
如何告知Hibernate使用Weblogic事務管理器并將它的事務與容器事務結合起來?我通過Google進行了幾分鐘的搜索找到了答案。向persistence.xml添加以下屬性。