Can you access EntityManagers from EntityListeners?

↘锁芯ラ 提交于 2019-12-07 07:57:49

问题


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

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

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

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?


回答1:


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.

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).

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

regards,



来源:https://stackoverflow.com/questions/336881/can-you-access-entitymanagers-from-entitylisteners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!