I have the following piece of code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test")
EntityManager entityManager = emf.createEntityManager()
User user = entityManager.find(User.class, 0);
entityManager.getTransaction().begin();
entityManager.getTransaction().rollback();
entityManager.refresh(user);
This throws an IllegalArgumentException on the fourth line saying "Entity not managed". If I change the third line to .commit()
instead of .rollback()
, everything seems to work fine.
What is going on here? Can I prevent this from happening?
UPDATE: @DataNucleus is directing me towards PersistenceContext. How do I change the persistence context in my code?