Hello,
I need to go through the following steps:
1. Load an entity from the database using Hibernate (works)
2. Update this entity using an HQL query (works)
3. Reloading the updated entity from the database (does not work! I only get the unchanged entity from step 1)
A simple example. I have the following entity:
Code:
class Event
{
int EventID;
Date Timestamp;
}
Now what I'm doing with Hibernate:
Code:
// Retrieve Entity
Event initialEvent = session.Load(1);
// Change Entity's Timestamp using HQL Query
Query q = session.CreateQuery("update Event set Timestamp = :Timestamp where EventID = :EventID");
q.SetInteger("EventID", 1);
q.SetDate("Timestamp", currentDate);
q.ExecuteUpdate();
// Retrieving the updated Entity again
Event updatedEvent = session.Load(1);
The third step, retrieving the updated Entity, does not work! At that point, updatedEvent contains the same Timestamp as initialEvent, but it should contain the Timestamp I updated with the HQL query. When debugging, I can see that in the last step, Hibernate does not even dispatch an SQL SELECT statement to fetch the updated data. I think this is because of some caching issue, Hibernate thinks that the entity hasn't changed because the update happened disconnected from it, and therefore doesn't bother to dispatch a fresh SQL query. How can I get Hibernate to retrieve the changed entity?
EDIT:
I found out that it works if I call
Quote:
session.clear()
after the HQL query. Unfortunately, this evicts all items from the cache which is not what I want.