I have the following code snippet:
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, which says: "Entity not managed". If I change the third line to .commit()
instead of .rollback()
, everything will work fine.
What's going on here? Can I prevent this?
UPDATE: @DataNucleus directs me to a PersistenceContext. How to change persistence context in my code?
source share