您可以从EntityListeners访问EntityManagers吗? [英] Can you access EntityManagers from EntityListeners?

查看:191
本文介绍了您可以从EntityListeners访问EntityManagers吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道JSR-000220 Enterprise JavaBeans 3.0最终版本(持久性)规范声明:

I'm aware that JSR-000220 Enterprise JavaBeans 3.0 Final Release (persistence) spec states:

通常,可移植应用程序不应调用EntityManager或Query操作,
访问其他实体实例,或修改生命周期回调方法中的关系。

"In general, portable applications should not invoke EntityManager or Query operations, access other entity instances, or modify relationships in a lifecycle callback method."

这看起来非常严格。我们有一种情况,我们想从EntityListener中访问EntityManager。

This appears extremely restrictive. We have a situation in which we would like to access the EntityManager from within an EntityListener.

在监听器中使用EntityManager时,是否有任何人遇到任何不利影响/ pitfulls在Jboss / Glassfish或任何其他应用程序服务器上?

Has anyone come across any adverse effects/pitfulls when using the EntityManager from within a Listener on Jboss/Glassfish, or any other application server for that matter?

推荐答案

事实上,你是对的。 JPA规范说它,在这种情况下,它建议EJB监听器(或非Java EE环境中的Spring AOP)。

In fact, you are right. JPA specification says it and, in that case, it suggests EJB listeners (or Spring AOP in non-Java EE environment) instead.

我不知道你的purporse监听器。但只是在其中加入了诸如日志记录,身份验证和路由等交叉问题。例如,Hibernate参考文档使用侦听器来审计所有对象修改的日志。一旦EJB侦听器可以使用实体管理器,它就可以由EJB侦听器(或非Java EE环境中的Spring AOP)完成。 Java EE规范允许它。因此,EJB侦听器共享其目标EJB使用的相同ENC,并且这样,也共享相同的持久性上下文(实体管理器)。

I do not know the purporse of your listener. But only put cross-cutting concerns like logging, authentication, and routing inside it. Hibernate reference documentation, for instance, uses listeners in order to audit log of all object modifications. It could be done by EJB listeners (or Spring AOP in non-Java EE environment) once EJB listeners can use entity manager. Java EE specification allows it. Therefore, EJB listeners share the the same ENC used by its target EJB and, this way, also share the same persistence context (entity manager).

如果您愿意,请参阅我的简历。我有许多与ORM相关的问题。

If you wish, see my profile. I have many ORM related issues.

问候,

这篇关于您可以从EntityListeners访问EntityManagers吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆