views:

275

answers:

3

I have an entity that has a state table associated with it. The state table is managed by another process, and contains a list of objects that my business logic must process. I would like to get a new snapshot of the state table each time I reload the entity. How can I ensure that no part of Hibernate or its support libraries ever caches any of the values of this table? Basically, I want to get a new view of the collection every time I call getMyStateValues ().

+1  A: 

Most of the point of Hibernate is to prevent that from happening and return a consistent view of an entity's state in the scope of a given transaction. So either reload the whole entity, in different transactions every time. Or, if you need to reload the state table during a business transaction, load only the state table by the parent entity's id in a separate Hibernate session.

Sii
Can I take that to mean that if I commit the transaction in progress, my next read of the collection will contain new values?
Chris R
I use Spring's transaction management, so I'm not sure if committing the transaction works or if you should create a new Session off the top of my head. It should clear the first-level cache however.The second-level cache might still kick in if you're using one though. The default should be not to use one.This should be easy enough to verify with a test app and a SQL console open on the same DB.
Sii
A: 

You can create a method in your entity that queries the database and return the collection. getXYXReload(). It´s not a very nice design decision, thought.

razenha
A: 

You can use Hibernate's CacheMode. It allows you to instruct a hibernate session on how to interact with the cache. You can get access to the underlying session with:

@PersistenceContext EntityManager manager;
...
org.hibernate.Session session = (Session)manager.getDelegate();

Unfortunately, this technique applies to the whole session, and not specifically to an entity.

Rich Kroll