基于容器的 EntityManager 的动态 @PersistenceContext unitName 属性
基本上,我想要做的就是为 @PersistenceContext 的 'unitName' 属性分配一个我将在运行时从会话中获取的值。
详细信息;
我的应用程序将是一个 SaaS 应用程序,我将为每个不同的租户拥有单独的数据库。我正在使用Glassfishv3并管理基于容器的实体,因此我没有明确从EntityManagerFactory获取任何实例。我为创建实体管理器所做的就是;
@PersistenceContext(unitName = "DBNAME")
private EntityManager entityManager;
我需要根据当前用户传递unitName属性。它不应该是硬编码的。
我已经更新了 Eclipselink 2.3,但所有示例都是从 EMF 创建一个实例,您可以传递属性映射,就像
Map memberProps = new HashMap();
memberProps.put("memberPu1", props1);
memberProps.put("memberPu2", props2);
Map props = new HashMap();
props.put("eclipselink.jdbc.exclusive-connection.mode", "Always");
props.put("eclipselink.composite-unit.properties", memberProps);
EntityManager em = emf.createEntityManager(props);
在我的应用程序中不太可能那样,容器会完成这项工作,所以我无法执行此操作
EntityManager em = emf.createEntityManager(props);
即使我有通过使用 JNDI 定义,我的 persistence.xml 中的所有持久性单元和类,我无法告诉应用程序服务器它应该使用哪个 DB(持久性单元),
任何帮助将不胜感激
basically, what i wanna do is assigning 'unitName' attribute of @PersistenceContext with a value that i will get from the Session at runtime.
in details;
my application will be a SaaS application and i will have separate DBs for every different tenants. i am using Glassfishv3 and managing the entities container-based so i dont get any instance from EntityManagerFactory explicitly. All i am doing for creating an entity manager is ;
@PersistenceContext(unitName = "DBNAME")
private EntityManager entityManager;
i need to pass unitName attribute according to the current user. it shouldn't be hard-coded.
I have updated Eclipselink 2.3 but all examples are creating an instance from EMF which you can pass property Map like
Map memberProps = new HashMap();
memberProps.put("memberPu1", props1);
memberProps.put("memberPu2", props2);
Map props = new HashMap();
props.put("eclipselink.jdbc.exclusive-connection.mode", "Always");
props.put("eclipselink.composite-unit.properties", memberProps);
EntityManager em = emf.createEntityManager(props);
unlikely in my app, container does that job so i am not being able to do this
EntityManager em = emf.createEntityManager(props);
Even though i have all the persistence units and classes in my persistence.xml by using JNDI definitions, i am not being able to tell app server which DB(persistence unit) it should use at the time
any help would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注释中的值无法在运行时分配,因此您需要找到一种可以创建多个 PersistenceContext 的策略。如果您可以使用 CDI,它可能会让您的生活更轻松。
使用 CDI,您可以按如下方式创建生产者:
然后您可以在 DAO 中使用生产者方法:
编辑:我可能会建议使用
@Qualifier
注释,因为它清楚地表明在哪里您是否从以下位置获取EntityManager
?Values in annotations cannot be assigned at runtime, and therefore you will need to find a strategy where you can create multiple
PersistenceContext
s. If you can use CDI, it will probably make your life easier.With CDI you might be able to create a producer as follows:
Then you can use your producer method in e.g. your DAO:
EDIT: I would probably recommend to use a
@Qualifier
annotation as it makes it clear where are you getting theEntityManager
from.您需要使用应用程序管理的持久性单元,而不是容器。
IE
Persistence.createEntityManagerFactory()
您仍然可以使用 JTA,只是不能注入。
You need to use an application managed persistence unit, not container.
i.e.
Persistence.createEntityManagerFactory()
You can still use JTA, just not injection.