從今天開始運行Glassfish 3.1-SNAPSHOT(2010-11-12)。當指定PU時,修復「無法解析持久性單元...」錯誤
我正在使用嵌入式EJBContainer。
在EJBContainer報告的類路徑上,我有一個META-INF/persistence.xml。這個文件定義了兩個持久性單元:一個叫做「ngp」,一個叫做「cx」。
調試輸出顯示Glassfish JPA部署者發現它,並識別cx PU和ngp PU。
的炸彈的EJBContainer出具有以下所有太常見的JPA錯誤:
java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [cx] in the scope of the module called [/Users/ljnelson/Projects/foo/target/test-classes/]. Please verify your application.
at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:693)
at com.sun.enterprise.deployment.EjbBundleDescriptor.findReferencedPUs(EjbBundleDescriptor.java:910)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:140)
at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:869)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:410)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.kernel.embedded.EmbeddedDeployerImpl.deploy(EmbeddedDeployerImpl.java:193)
at org.glassfish.kernel.embedded.EmbeddedDeployerImpl.deploy(EmbeddedDeployerImpl.java:142)
at org.glassfish.ejb.embedded.EJBContainerImpl.deploy(EJBContainerImpl.java:135)
at org.glassfish.ejb.embedded.EJBContainerProviderImpl.createEJBContainer(EJBContainerProviderImpl.java:132)
at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:127)
我再次強調,部署日誌顯示至少部署遇到兩個持久化單元。
想要使用的 「CX」 之類的PU包含了常用的樣板:
@PersistenceContext(unitName="cx")
private EntityManager em;
persistence.xml中存在於(通常的Maven的地方)target/test-classes/META-INF
,看起來像這樣:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="cx" transaction-type="JTA">
<jta-data-source>java:global/jdbc/H2Test</jta-data-source>
<!-- snip -->
</persistence-unit>
<persistence-unit name="ngp" transaction-type="JTA">
<jta-data-source>java:global/jdbc/H2Test</jta-data-source>
<!-- snip -->
</persistence-unit>
</persistence>
Glassfish的嵌入式的EJBContainer,同時做其工作,這個輸出:
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINE: Got [email protected]
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINER: With attribute name
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINER: With value cx
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINE: in class com.sun.enterprise.deployment.PersistenceUnitDescriptor method setName with cx
...snip...
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINE: Got [email protected]
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINER: With attribute name
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINER: With value ngp
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINE: in class com.sun.enterprise.deployment.PersistenceUnitDescriptor method setName with ngp
疑難解答食譜,任何人?
我應該提到我正在使用JTA - 容器管理事務 - 在兩個PU中。我發現XA在這裏是一個要求。我的測試數據源不符合XA的事實是否會導致此錯誤? – 2010-11-12 18:13:29
已更新數據源以使用XA;沒有效果。 – 2010-11-12 18:26:32
數據源是否是XA應該無關緊要,至少不是現在(如果您決定同時使用兩個EM,可能會晚一些,但這是另一回事)。它只適用於一個PU嗎? – 2010-11-12 18:57:57