2011-03-18 90 views
10

我们创建了一些我们所有项目都会使用的库,这些库将提供我们所有系统(登录,管理等)的基本功能。但应用程序本身可以使用另一个数据库。Persistence.xml中的两个持久化单元

我们所做的是使用两个持久单元创建Persistence.xml。并将所有核心库实体打包在一个名为“LN-model.jar”的jar文件中,并将所有“out-test app”实体放在“App-model.jar”中。但由于某种原因,我们仍然获得以下信息。

无法解析对应于持久性上下文REF-名称[xxxxlistener.InicializadorListener/EM]在被称为[gfdeploy#/用户/ zkropotkine/WORK/SeguridadCore/DIST模块的范围持久化单元/ gfdeploy/SeguridadCore-war_war]。请验证您的申请。

这是我们的persistence.xml

<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="x" transaction-type="JTA"> 
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
    <jta-data-source>jdbc/x</jta-data-source> 
    <jar-file>App-model.jar</jar-file> 
    <exclude-unlisted-classes>false</exclude-unlisted-classes> 
    <properties> 
    </properties> 
</persistence-unit> 

<persistence-unit name="y" transaction-type="JTA"> 
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
    <jta-data-source>jdbc/y</jta-data-source> 
    <jar-file>LN-model.jar</jar-file> 
    <exclude-unlisted-classes>false</exclude-unlisted-classes> 
    <properties/> 
</persistence-unit> 

通过我们把persistence.xml中在一个罐子里的路上,我们加入到我们的企业项目(EAR)。

+0

您正在使用哪台服务器? – Premraj 2011-03-18 19:02:07

+0

玻璃鱼2.1.1 – zkropotkine 2011-03-18 20:50:44

回答

10

问题是JPA不知道哪个是要使用的持久性单元。当你只有一个持久单元时,这个问题不会发生。要解决做到以下几点:

你需要指定一个持久性单元:@PersistenceContext(的unitName =“...”)在EJB没有

7

您可以添加注释:

@PersistenceUnit(name = "x") 
EntityManagerFactory entityManagerFactory; 

@PersistenceContext(unitName = "y") 
EntityManager entityManager; 

或者你可以手动创建:

EntityManagerFactory emfA = Persistence.createEntityManagerFactory("x", properties); 
EntityManagerFactory emfB = Persistence.createEntityManagerFactory("y", properties); 

有关详细信息,请参阅以下链接:https://docs.oracle.com/html/E25034_01/usingmultipledbs.htm 是非常有用的,对我帮助我!