I'm using jboss, and I have a class using Hibernate criteria query to execute Entity read from a db.
I've implemented the query with
// org.hibernate.Criteria crit ..
// crit.add(Restriction...
// crit.setProjections(...
List<MyObject> l = crit.list()
during the application execution, another application change the db content . So If I read again in the same session with another c.list() the data seems not changed, because the hibernate cache returns to me the 'old' data.
With refresh I can do something like (starting from EntityManager em to get Session):
for (MyObject o : l) {
((Session)em.getDelegate()).refresh(o);
}
It works, it refresh the object content, with the updated data coming from the db instead of from the cache. But this is highly inefficient, because it refresh one by one all the object in the list, and they could be a lot.
Another way is close and reopen the session, but I dont' wont do it.
I tried with ((Session)em.getDelegate()).setCacheMode(CacheMode.REFRESH)
and with crit.setCacheable(false)
but in both cases all the read execution returns to me the 'old' version of the data from the cache.
Do you have any suggstion, to set in the session / criteria to execute the query from the db, instead of from the cache? In my application usage the cache is ok, except in one case as explained above, where the data on the db are changed from outside, from another session of another application.
Thank you