8

persistence.xml在使用 struts2 ejb hibernate 的 Web 应用程序中,是否可以在部署时告诉应用程序为特定的持久性单元名称(写入文件中)查找或创建实体?

persistence.xml在 jboss 节点下有两个持久化单元和一个数据源(包括两个“local-tx-datasource”)xml 文件。

为了澄清,我的意思是,我试过这个;

@Entity  
@PersistenceContext(unitName="MY JNDI NAME specified in persistence.xml") 
public abstract class Vehicle {

并且不起作用..然后尝试了这个等等..

@PersistenceContext(name="MY PERSISTENCE UNIT NAME specified in persistence.xml")

@PersistenceUnit(name="MY PERSISTENCE UNIT NAME specified in persistence.xml")

而且我在上面用“UnitName = ..”而不是“name = ..”尝试了这些,但任何东西都对我有用......

[解决了]

<.exclude-unlisted-classes>true<./exclude-unlisted-classes> 解决了我的问题

4

3 回答 3

9

更新:根据您的评论(这不是我从原始问题中所理解的),我认为您除了禁用“发现”并在各自的持久性单元中明确列出您的实体之外别无选择:

<?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>

我不知道实体级别的任何语法允许将其分配给持久性单元。


我不确定我是否理解您要执行的操作,但是如果您想为注入的特定持久性单元获取实体管理器,您应该这样做:

@Stateless
public class FooBean implements Foo {
    @PersistenceContext(unitName="MyPu1")
    EntityManager em1;

    // ...
}

如果这不是您想要的,请澄清问题。

于 2010-08-05T16:26:47.350 回答
0

您正在寻找的可能是<exclude-unlisted-classes>true</exclude-unlisted-classes>.

检查 jboss.org 上的文档:

在我的配置中,我有两个数据库(比如说 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>

于 2016-12-13T12:38:05.057 回答
-1

@Pascal Thivent

我没有尝试一次使用多个 EntityManager,但是看看上面提到的问题,如果它有效,这可能会有所帮助。

@PersistenceContext(unitName="MyPu1") EntityManager em1;

@PersistenceContext(unitName="MyPu2") EntityManager em2;

于 2010-08-05T18:24:18.927 回答