views:

342

answers:

1

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
it doesnt work, EntityManager is null.
01
mmmmmm.... weird. it works for me. what's your JPA implementation? Hibernate? What's your container? JBoss? Tomcat? Weblogic?
Pablo Santa Cruz
@PersistenceContext annotation works standard only in a EJB server.
Kdeveloper