如何将实体绑定到特定的持久单元
在使用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 = ..”,但任何东西都对我有用...
[已解决]< /strong>
<.排除未列出的类>true<./排除未列出的类> 解决了我的问题
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.xml
file, in the deployment time?
I have two persistence-unit in persistence.xml
, and one datasource
(including two "local-tx-datasource") xml file under the jboss node.
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...
[SOLVED]
<.exclude-unlisted-classes>true<./exclude-unlisted-classes>
has solved my problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新:根据您的评论(这不是我从原始问题中理解的),我认为除了禁用“发现”并在各自的持久性中明确列出您的实体之外,您没有其他选择单元:
我不知道实体级别有任何语法允许将其分配给持久性单元。
我不确定我是否理解您要做什么,但如果您想为注入的特定持久性单元获取实体管理器,您应该这样做:如果这不是您想要的想要,请澄清问题。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:
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:If this is not what you want, please clarify the question.您正在寻找的可能是
true
。检查 jboss.org 上的文档:
在我的配置中,我有两个数据库(假设 A 和 B),并且我想要两个单独的持久性单元,其中一个包含所有实体,但另一个持久性单元包含其余实体。我的 persistence.xml 如下所示:
What you're looking for is probably
<exclude-unlisted-classes>true</exclude-unlisted-classes>
.Check the documentation on 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:
@Pascal Thivent
我还没有尝试一次使用多个EntityManager,但是看看上面提到的问题,如果它有效的话可能会有所帮助。
@PersistenceContext(unitName="MyPu1")
EntityManager em1;
@PersistenceContext(unitName="MyPu2")
EntityManager em2;
@Pascal Thivent
I haven't tried using multiple EntityManager at once, but looking at above mentioned problem this may help if it works.
@PersistenceContext(unitName="MyPu1")
EntityManager em1;
@PersistenceContext(unitName="MyPu2")
EntityManager em2;