I want to save history of changes, so in @PostUpdate i want to create new instance of another entity and save it, how do i pass EntityManager to that method?
+2
A:
You can get an instance of EntityManager just by retrieving it from the context as you would on any other:
1) Through injection in your class:
@PersistenceContext EntityManager em; // injected in your class
2) Using EntityManagerFactory:
EntityManager em = EntityManagerFactory.getEntityManager();
Pablo Santa Cruz
2009-05-03 17:24:02
it doesnt work, EntityManager is null.
01
2009-05-03 17:35:07
mmmmmm.... weird. it works for me. what's your JPA implementation? Hibernate? What's your container? JBoss? Tomcat? Weblogic?
Pablo Santa Cruz
2009-05-03 17:38:05
@PersistenceContext annotation works standard only in a EJB server.
Kdeveloper
2010-09-02 18:35:30