-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to reload an entity which was updated through HQL query
PostPosted: Wed Jun 02, 2010 9:54 am 
Newbie

Joined: Wed Jun 02, 2010 9:30 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: How to reload an entity which was updated through HQL query
PostPosted: Wed Jun 02, 2010 11:01 am 
Regular
Regular

Joined: Tue May 11, 2010 5:50 pm
Posts: 54
Location: Norman, Ok, U.S.A
Try using session.flush() and then instead of load use get()


Top
 Profile  
 
 Post subject: Re: How to reload an entity which was updated through HQL query
PostPosted: Wed Jun 02, 2010 11:15 am 
Newbie

Joined: Wed Jun 02, 2010 9:30 am
Posts: 2
Hello,

thanks for your quick reply. Unfortunately, this doesn't solve the problem. It's the same like before.


Top
 Profile  
 
 Post subject: Re: How to reload an entity which was updated through HQL query
PostPosted: Wed Jun 02, 2010 12:02 pm 
Regular
Regular

Joined: Tue May 11, 2010 5:50 pm
Posts: 54
Location: Norman, Ok, U.S.A
Do you see changes in the database, because I don't see the commit here? Do you have auto commit set to true?


Top
 Profile  
 
 Post subject: Re: How to reload an entity which was updated through HQL query
PostPosted: Wed Jun 02, 2010 2:56 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
session.refresh(initialEvent) will re-load the state from the database.

If you want to keep the initialEvent as it is and instead get a new object you can do:

Code:
session.evict(initialEvent);
Event updatedEvent = (Event)session.load(Event.class, 1)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.