java 如何将实体绑定到特定的持久性单元
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3415682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind an entity to a specific persistence-unit
提问by Javatar
In a web application using struts2 ejb hibernate, is it possible to tell the application to find or create an entity for a specific persistence-unit name, which is written in persistence.xmlfile, in the deployment time?
在使用 struts2 ejb hibernate 的 web 应用程序中,是否可以persistence.xml在部署时告诉应用程序为特定的持久性单元名称查找或创建实体,该名称写入文件?
I have two persistence-unit in persistence.xml, and one datasource
(including two "local-tx-datasource") xml file under the jboss node.
我persistence.xml在 jboss 节点下有两个持久性单元和一个数据源(包括两个“local-tx-datasource”)xml 文件。
To clearify, I mean, I tried this;
为了澄清,我的意思是,我试过这个;
@Entity
@PersistenceContext(unitName="MY JNDI NAME specified in persistence.xml")
public abstract class Vehicle {
and doesnt work.. Then tried this and etc..
并不起作用..然后尝试了这个等等..
@PersistenceContext(name="MY PERSISTENCE UNIT NAME specified in persistence.xml")
@PersistenceUnit(name="MY PERSISTENCE UNIT NAME specified in persistence.xml")
and also I tried these above with the "UnitName=.." instead of "name=.." but anything is worked for me...
而且我在上面用“UnitName = ..”而不是“name = ..”尝试了这些,但任何东西都对我有用......
[SOLVED]
[解决了]
<.exclude-unlisted-classes>true<./exclude-unlisted-classes> has solved my problem
<.exclude-unlisted-classes>true<./exclude-unlisted-classes> 解决了我的问题
回答by Pascal Thivent
Update:Based on your comment (this is not what I understood from the original question), I don't think you have any other option than disabling "discovery" and listing explicitly your entities in their respective persistence unit:
更新:根据您的评论(这不是我从原始问题中所理解的),我认为除了禁用“发现”并在各自的持久性单元中明确列出您的实体之外,您别无选择:
<?xml version="1.0" encoding="UTF-8"?>
<persistence
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"
version="2.0">
<persistence-unit name="MyPu1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mycompany.Foo</class>
...
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- H2 in memory -->
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
<property name="javax.persistence.jdbc.username" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
<persistence-unit name="MyPu2" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mycompany.Bar</class>
...
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Derby server -->
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Pu2;create=true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
I'm not aware of any syntax at the entity level allowing to assign it to a persistence unit.
我不知道实体级别的任何语法允许将其分配给持久性单元。
I'm not sure I understood what you're trying to do but if you want to get an Entity Manager for a specific persistence unit injected, you should do:
我不确定我是否理解您要执行的操作,但是如果您想为特定持久性单元注入实体管理器,您应该执行以下操作:
@Stateless
public class FooBean implements Foo {
@PersistenceContext(unitName="MyPu1")
EntityManager em1;
// ...
}
@Stateless
public class FooBean implements Foo {
@PersistenceContext(unitName="MyPu1")
EntityManager em1;
// ...
}
If this is not what you want, please clarify the question.
如果这不是您想要的,请澄清问题。
回答by Sylwester Gebczyk
What you're looking for is probably <exclude-unlisted-classes>true</exclude-unlisted-classes>.
您正在寻找的可能是<exclude-unlisted-classes>true</exclude-unlisted-classes>.
Check the documentation on jboss.org:
查看 jboss.org 上的文档:
In my configuration I had two databases (let's say A and B) and I wanted two separate persistence units, where one contains all entities, but one, while the other persistence unit contains the remaining entity. My persistence.xml looks like this:
在我的配置中,我有两个数据库(比方说 A 和 B),我想要两个单独的持久性单元,其中一个包含所有实体,但一个,而另一个持久性单元包含剩余的实体。我的persistence.xml 看起来像这样:
<persistence 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"
version="2.0">
<persistence-unit name="A" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="${hibernate.dialect}" />
</properties>
</persistence-unit>
<persistence-unit name="B" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="${rud.hibernate.dialect}"/>
</properties>
<class>com.sgk.Person</class>
</persistence-unit>
回答by Nayan Wadekar
@Pascal Thivent
@Pascal Thivent
I haven't tried using multiple EntityManager at once, but looking at above mentioned problem this may help if it works.
我还没有尝试一次使用多个 EntityManager,但是查看上面提到的问题,如果它有效,这可能会有所帮助。
@PersistenceContext(unitName="MyPu1")
EntityManager em1;
@PersistenceContext(unitName="MyPu1")
EntityManager em1;
@PersistenceContext(unitName="MyPu2")
EntityManager em2;
@PersistenceContext(unitName="MyPu2")
EntityManager em2;

