Hi All,
I would like to introduce a LifecycleListener and Lifecycle Call Back methods like:
@PostAttach
public void postAttach(EntityManager manager){
}
@PreDetach
public void preDetach(){}
Reason:
In rich domain entities it is often required to get a reference to an EntityManager to be able to create and persist new entities or to perform queries.
Currently it is only possible with workarounds like using ThreadLocal or ThreadSynchronizationRegistry. With an extension of the lifecycle listener you would get
a reference to an EntityManager and will get notified in case it is no more valid.
Injection of the EntityManager would be the best/easiest solution, however JPA-entities are managed by the EntityManager and not by the container...
I have the issue in every non-trivial project with a rich domain object model. There are, however, no issues with an anemic (
http://www.martinfowler.com/bliki/AnemicDomainModel.html) model, because usually the EJBs / CDI managed beans do implement the business logic.
see also:
http://www.adam-bien.com/roller/abien/entry/why_sometimes_the_entitymanager_in
best regards,
adam