在使用 struts2 ejb hibernate 的 Web 应用程序中,是否可以告诉应用程序为特定的持久性单元名称查找或创建实体,该名称写在 persistence.xml
文件中,在部署时间?
我在 persistence.xml
中有两个持久化单元和一个数据源
(包括两个“local-tx-datasource”)jboss节点下的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> 解决了我的问题
最佳答案
更新:根据您的评论(这不是我从原始问题中了解到的内容),我认为除了禁用“发现”并明确列出您的实体外,您别无选择他们各自的持久化单元:
<?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>
我不知道实体级别的任何语法允许将其分配给持久性单元。
我不确定我是否理解您要执行的操作,但如果您想为注入(inject)的特定持久性单元获取实体管理器,您应该这样做:
@Stateless
public class FooBean implements Foo {
@PersistenceContext(unitName="MyPu1")
EntityManager em1;
// ...
}
如果这不是您想要的,请澄清问题。
关于java - 如何将实体绑定(bind)到特定的持久性单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3415682/