Force refresh of collection JPA entityManager

一曲冷凌霜 提交于 2019-11-27 13:57:26

You're having two separate problems here. Let's take the easy one first.


javax.ejb.EJBTransactionRolledbackException: Entity not managed

The List of objects returned by that query is not itself an Entity, and so you can't .refresh it. In fact, that's what the exception is complaining about. You're asking the EntityManager to do something with an object that is simply not a known Entity.

If you want to .refresh a bunch of things, iterate through them and .refresh them individually.


Refreshing the list of ItemStatus

You're interacting with Hibernate's Session-level cache in a way that, from your question, you don't expect. From the Hibernate docs:

For objects attached to a particular Session (i.e., in the scope of a Session)... JVM identity for database identity is guaranteed by Hibernate.

The impact of this on Query.getResultList() is that you do not necessarily get back the most up to date state of the database.

The Query you run is really getting a list of entity IDs that match that query. Any IDs that are already present in the Session cache are matched up to known entities, while any IDs that are not are populated based on database state. The previously known entities are not refreshed from the database at all.

What this means is that in the case where, between two executions of the Query from within the same transaction, some data has changed in the database for a particular known entity, the second Query would not pick up that change. It would, however, pick up a brand new ItemStatus instance (unless you were using a query cache, which I assume you're not).

Long story short: With Hibernate, whenever you want to, within a single transaction, load an entity and then pick up additional changes to that entity from the database, you must explicitly .refresh(entity).

How you want to deal with this depends a bit on your use case. Two options I can think of off the bat:

  1. Have are to have the DAO tied to the lifespan of the transaction, and lazily initialize the List<ItemStatus>. Subsequent calls to DAO.refreshList iterate through the List and .refresh(status). If you also need newly added entities, you should run the Query and also refresh the known ItemStatus objects.
  2. Start a new transaction. Sounds like from your chat with @Perception though that that is not an option.

Some additional notes

There was discussion about using query hints. Here's why they didn't work:

org.hibernate.cacheable=false This would only be relevant if you were using a query cache, which is only recommended in very particular circumstances. Even if you were using it though, it wouldn't affect your situation because the query cache contains object IDs, not data.

org.hibernate.cacheMode=REFRESH This is a directive to Hibernate's second-level cache. If the second-level cache were turned on, AND you were issuing the two queries from different transactions, then you would have gotten stale data in the second query, and this directive would have resolved the problem. But if you're in the same Session in the two queries, the second level cache would only come in to play to avoid database loading for entities that are new to this Session.

You have to refersh entity which was returned by entityManager.merge() method, something like:

@Override
public void refreshCollection(List<T> entityCollection){
    for(T entity: entityCollection){
        if(!this.getEntityManager().contains(entity)){
            this.getEntityManager().refresh(this.getEntityManager().merge(entity));
        }
    }
}

This way you should get rid of javax.ejb.EJBTransactionRolledbackException: Entity not managed exception.

UPDATE

Maybe it's safer to return new collection:

public List<T> refreshCollection(List<T> entityCollection)
    {
        List<T> result = new ArrayList<T>();
        if (entityCollection != null && !entityCollection.isEmpty()) {
            getEntityManager().getEntityManagerFactory().getCache().evict(entityCollection.get(0).getClass());
            T mergedEntity;
            for (T entity : entityCollection) {
                mergedEntity = entityManager.merge(entity);
                getEntityManager().refresh(mergedEntity);
                result.add(mergedEntity);
            }
        }
        return result;
    }

or you can be more effective if you can access entity IDs like this:

public List<T> refreshCollection(List<T> entityCollection)
    {
        List<T> result = new ArrayList<T>();
        T mergedEntity;
        for (T entity : entityCollection) {
            getEntityManager().getEntityManagerFactory().getCache().evict(entity.getClass(), entity.getId());
            result.add(getEntityManager().find(entity.getClass(), entity.getId()));
        }
        return result;
    }

Try marking it @Transactional

@Override
@org.jboss.seam.annotations.Transactional
public void refreshList(){
    itemList = em.createQuery("...").getResultList();
}

One of the option - bypass JPA cache for a specific query results:

    // force refresh results and not to use cache

    query.setHint("javax.persistence.cache.storeMode", "REFRESH");

many other tuning and configuration tips could be found on this site http://docs.oracle.com/javaee/6/tutorial/doc/gkjjj.html

After debugging it properly, I came across that one of the developers in my team injected it in the DAO layer as-

@Repository
public class SomeDAOImpl

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;

So, to consolidate it was getting cached and the query use to return the same stale data even though data has been changed in one of the column of the tables involved in the native sql query.

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