We are using Hibernate 3.1 with Spring MVC 2.0. Our problem occurs when data is updated on the database directly (not in the application). We use a Filter to filter a collection of results by whether the orders are opened or closed. If we change an order on the DB to be closed, the filter returns the correct list, however, the object’s status does not change.
- We do not have second-level or query caching enabled.
- The retrieval of the collection from the DB is via Hibernate’s Session.createQuery.
- Also, we have one SessionFactory wired, and using one Session throughout the application.
The only time the object displays the correct status result is when the server is restarted–which we’d prefer not to have to do on a regular basis.
The Session always has a ‘first-level’ cache, so if you’re using one Session everything you read through it is going to be cached. Hibernate will execute the query against the database, but then as it’s building the objects it checks the Session cache to avoid building a new object, so any columns changed in the database will not be refreshed. If you close it and get a new Session, it will read the full object from the database on the next query.