I need to inject EntityManager in EntityListener class so that I can perform CRUD operation on it.
POJO:
@Entity
@EntityListner(AuditLogging.class)
c
Anyways, I got this done by getting entityManager reference from EntityManagerFactory
bean which is configured in my jdbc-config.xml
. But again this is not what I wanted. I wanted to work around with @PersistenceContext
.
@Autowired
EntityManagerFactory entityManagerFactory;
private static EntityManager entityManager;
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
entityManager=entityManagerFactory.createEntityManager();
this.entityManagerFactory = entityManagerFactory;
}
Here are few notes that we need to keep in mind:
EntityManager
into an EntityListener
(through
@PersistenceContext
). EntityListener
is not managed by any of the
containers@PersistenceContext
class cannot be static. So we cant
attain the instance while class loading. EntityManager