代码之家  ›  专栏  ›  技术社区  ›  javatar

如何将实体绑定到特定的持久性单元

  •  8
  • javatar  · 技术社区  · 15 年前

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

    我有两个坚持单位 坚持XML 和一个数据源 (包括jboss节点下的两个“本地Tx数据源”)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=”,尝试了上面的方法,但是任何方法都对我有效…

    [解决]

    <。排除未列出的类>true<。/排除未列出的类> 解决了我的问题

    3 回复  |  直到 8 年前
        1
  •  9
  •   Pascal Thivent    15 年前

    更新: 基于您的评论(这不是我从原始问题中理解的),我认为除了禁用“发现”并在其各自的持久性单元中明确列出您的实体之外,您没有其他任何选择:

    <?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;
    
        // ...
    }
    

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

        2
  •  0
  •   Sylwester Gebczyk    8 年前

    你要找的可能是 <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>
    

        3
  •  -1
  •   Nayan Wadekar    15 年前

    @ Pascal Thivent

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

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

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

    推荐文章