-->
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.  [ 11 posts ] 
Author Message
 Post subject: Understanding Session.refresh(Object)
PostPosted: Thu Oct 09, 2003 12:36 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
According to the JavaDoc on Session.refresh(Object):
Code:
   /**
    * Re-read the state of the given instance from the underlying database. It is
    * inadvisable to use this to implement long-running sessions that span many
    * business tasks. This method is, however, useful in certain special circumstances.
    * For example
    * <ul>
    * <li>where a database trigger alters the object state upon insert or update
    * <li>after executing direct SQL (eg. a mass update) in the same session
    * <li>after inserting a <tt>Blob</tt> or <tt>Clob</tt>
    * </ul>

it should re-read the "state" of the given instance from the underlying database. Particularly, this should be useful after executing direct SQL (eg. a mass update) in the same session.

Now here's my question, if I opened a new session and used it to run a stored procedure which inserted a lot of records, then flushed and closed that session, then afterwards used a ThreadLocal session from a Filter to refresh an object which should contain the new records as part of a collection should I expect to see the new records or not? At present I do not and I am guessing this is because the ThreadLocal session does not see the changes, is this correct? In order for the Session.refresh to be able to see the inserted records I would need to have used the same session to perform the call to the stored procedure is that correct?

Thanks,
David


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 12:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes, if session1 and session2 are independant, refreshing session2 won't have any impact on session1.


Top
 Profile  
 
 Post subject: new take
PostPosted: Mon Oct 20, 2003 12:50 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Okay, I'm using the same session to call the stored procedure and then afterwards to call .refresh(myObject) but I still don't see the records that were inserted by the stored procedure.
The session has been flushed after the running the SP.

Should the Session.refresh method also refresh any collections on the object or just the root object itself?

I can hack around my problem by evicting the ojbect (don't know if I actually need to?) and then opening a new session and reloading it, closing that new session, and then reassociating the object with the ThreadLocal Session before rendering the view. This seems a little bit weird though.

Is that an appropriate approach or did I miss the boat completely?

Thank-you.


Top
 Profile  
 
 Post subject: Re: new take
PostPosted: Mon Oct 20, 2003 3:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
DavidNDuffy wrote:
The session has been flushed after the running the SP.

Beware ! If you changed your object before sp call, first object changes will overide sp's during flush()

DavidNDuffy wrote:
Should the Session.refresh method also refresh any collections on the object or just the root object itself?
Quote:
Session.refresh(obj); evit collections but not explicitly reload then (they won't be reloaded, in case of lazy=true etc...)

DavidNDuffy wrote:
I can hack around my problem by evicting the ojbect (don't know if I actually need to?) and then opening a new session and reloading it, closing that new session, and then reassociating the object with the ThreadLocal Session before rendering the view. This seems a little bit weird though.
Quote:
You need to evict it to allow thread local session not to saveOrUpdate a transient object that already persists in session.

DavidNDuffy wrote:
Is that an appropriate approach or did I miss the boat completely?

Should be another way. hope first answers will help you to find out why refresh doen't work as expected.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 9:18 am 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
Since the documentation is not explicit on this, I thought I'd post the simple definitive answer:

session.refresh() does not descend into collections (other than to evict() them).


If you want to reload the state of an entire object graph from the database, you must manually traverse and call refresh() on each object (remember to check the object is not transient before calling refresh or you will get a null identifier exception).

Ben


Top
 Profile  
 
 Post subject: How is session.refresh(Object) working ?
PostPosted: Tue Feb 08, 2005 4:02 am 
Newbie

Joined: Wed Feb 02, 2005 3:01 am
Posts: 9
Emmanuel, I think we need here to describe how session.refresh(Object) works? does it extends to all the hierarchy of Object automatically OR should hibernate user dig inside it & refresh each memebr object one by one ?!!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 04, 2005 11:01 am 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
Update: as of newer releases of hibernate, session refresh now DOES seem to refresh collections.

Our jUnit test "testRefreshIsNotRecursive" succeeds with 3.0.1 but fails when we upgraded to 3.0.5.

...so now we have a new jUnit test "testRefreshIsRecursive" ;)

The changelog suggests this was changed in 3.0.3:

Quote:
Changes in version 3.0.3 (8.5.2005)
* fixed issue with non-cascading refresh to collections


Ben


Top
 Profile  
 
 Post subject: I am having same problem...
PostPosted: Fri Jul 08, 2005 2:49 pm 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
I am having same problem... This is a critical issue in my app. I had to downgrade to 3.0.2.


What's the story with this..


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 09, 2005 7:20 am 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
The story seems to be that now it does what it says on the tin.

Shame it didnt do this before because I had to waste a lot of time writing quite a bit of generic tree traversal code (to implement my own recursive refresh).


Top
 Profile  
 
 Post subject: ok..
PostPosted: Sat Jul 09, 2005 8:49 am 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
So how do you refresh the collections so there is no null pointer exception accesing them.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 09, 2005 8:55 am 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
I dont get any NPEs. It works fine for me using 3.0.5. Collections are correctly refreshed. Are you using the latest version 3.0.5? Have you created a simple JUnit test?

If you still have trouble, you will need to post your hib version, mapping file, code etc to the forum and maybe someone will be able to help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.