|
As states in Hibernate documentation:
AUTO - The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.
In the attached unit test I tested if this assumption stands still when an Entity is persisted and we are issuing an SQL query on the same database table:
Product product = new Product("LCD"); session.persist(product); assertEquals(product.getId(), session.createSQLQuery("select id from product").uniqueResult());
The Product entity has an UUID2 identifier, so the id is generated right from the start. The entity resides in the 1st level cache waiting to be synchronized with the database (during the flush time).
If I ran the equivalent HQL query, a flush need is detected and the query will return consistent data.
This doesn't happen with the SQLQuery, and because AUTO only flushes "sometimes" the current event query space is empty:
private boolean flushIsReallyNeeded(AutoFlushEvent event, final EventSource source) { return source.getActionQueue() .areTablesToBeUpdated( event.getQuerySpaces() ) || source.getFlushMode()==FlushMode.ALWAYS; }
The flushIsReallyNeeded returns false in this case.
|