Search code examples
javahibernatejpaentitylisteners

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


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.


Solution

  • 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.