Hibernate JPA Entity listener @Pre and @Post don't work as expected

有些话、适合烂在心里 提交于 2019-12-02 13:26:38

问题


I'm building a real time app and trying to use entity listeners to keep my state up to date. The basic idea is that whenever an area of business logic changes, I re-load the affected entities and reconcile the changes. Here's a MWE:

@PrePersist
public void PrePersist() {
    LoggerFactory.logger(App.class).info(" >>> PrePersist count: " + getStars().size());
}
@PostPersist
public void PostPersist() {
    LoggerFactory.logger(App.class).info(" >>> PostPersist count: " + getStars().size());
}
@PreRemove
public void PreRemove() {
    LoggerFactory.logger(App.class).info(" >>> PreRemove count: " + getStars().size());
}

@PostRemove
public void PostRemove() {
    LoggerFactory.logger(App.class).info(" >>> PostRemove count: " + getStars().size());
}
private List<Star> getStars() {
    EntityManager em = HibernateUtilJpa.getEntityManager();
    List<Star> l = new ArrayList<Star>();
    try {
        em.getTransaction().begin();
        l = em.createQuery("from Star", Star.class).getResultList();
        em.getTransaction().commit();
    } catch (Exception e) {
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
    return l;
}

I'm using a separate API to insert/remove stars into DB. I was expecting that post-persist would show one item more because of the added item, and post-remove would be one fewer than pre-remove. This is not the case, both post-persist and post-remove show the incorrect number of items, so pre-persist is the same as post-persist, and pre-remove is the same as post-remove. I'm sure it has to do with Hibernate-s caching, but I'm using transactions and everything goes through the EntityManager so I'm scratching my head.


回答1:


From the documentation:

A callback method must not invoke EntityManager or Query methods!

In practice, the behaviour if you do it is undefined, hence the results you observe in your examples.



来源:https://stackoverflow.com/questions/39312078/hibernate-jpa-entity-listener-pre-and-post-dont-work-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!