This post is in continuation of JPA How to get the value from database after persist
When I execute the following I am getting following exception, how can I resolve
Injecting EntityManagerFactory instead of EntityManager and javax.transaction.Transactional annotation on method solved my issue as shown below.
//Autowire EntityManagerFactory
@PersistenceUnit(unitName = "readwrite.config")
private EntityManagerFactory entityManagerFactory;
//Use below code on create/update
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
if (!ObjectUtils.isEmpty(entity) && !entityManager.contains(entity)) {
entityManager.persist(entity);
entityManager.flush();
}
entityManager.getTransaction().commit();