-2

Simply I want to clear the peristence contexte of my entity manager to reload ALL data from database and specially in that method:

public MyEntity find(Object id) {
    EntityManager em = getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.flush();
    tx.commit();
    em.clear();
    return em.find(MyEntity .class, id);
}

But it seems that flush() is not working properly because when I insert an OtherEntity and execute that method I don't find it in the persistence context by getCollection() method (only after restarting my app)..

Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
  • 1
    em.flush just before commit is totally meaningless ... commit does a flush. Your transaction has nothing being performed within it, so that is also totally meaningless. So look at the log if you want to debug your problem. I see no getCollection in your posted code (whatever getCollection is) – Neil Stockton Feb 22 '16 at 08:25

1 Answers1

0

I found the answer :

@Override
public MyEntity find(Object id) {
    EntityManager em = getEntityManager();
    MyEntity ps = em.find(MyEntity .class, id);
    em.refresh(ps);
    return ps;
}
Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
  • 1
    This means you are not maintaining both sides of your bidirectional relationships. When you make changes, you need to update both sides or they become out of synch, reducing the value of caching. See http://stackoverflow.com/questions/13258976/how-to-refresh-jpa-entities-when-backend-database-changes-asynchronously if you are making changes outside of JPA and want to clear the second level cache. – Chris Feb 22 '16 at 14:41