clear() 和 flush() 在 JPA/EclipseLink 中不起作用

clear() and flush() not working in JPA/EclipseLink

只是我想清除我的实体管理器的持久性上下文以从数据库重新加载所有数据,特别是在该方法中:

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);
}

flush() 似乎无法正常工作,因为当我插入 OtherEntity 并执行该方法时,我没有通过 getCollection() 方法在持久性上下文中找到它(仅在重新启动我的应用程序后)..

我找到了答案:

@Override
public MyEntity find(Object id) {
    EntityManager em = getEntityManager();
    MyEntity ps = em.find(MyEntity .class, id);
    em.refresh(ps);
    return ps;
}