-->
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.  [ 10 posts ] 
Author Message
 Post subject: How to refresh a collection after some save/delete?
PostPosted: Thu Dec 21, 2006 11:33 am 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
I have a question for refresh a collection.
In my program, I first read some data from database by
session.createQuery("from GroupRelation").list();

Then, I make some insert into the GroupRelation table by session.save(object). However, I find I must requery the table, then I can find the insert result in the collection. Otherwise, even I used flush()/refresh(object) or load(), they can not make the inserted object in the collection.

Is there any other way can make the collection has the same content with database table?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 12:59 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
When encountering much problems with synchronizing the cache like here, maybe consider calling session.clear() at least once?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:02 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Oh, sorry, I reread more cautiously what you said. You want Hibernate to update dynamically a collection because you inserted something into the corresponding table?

IMO, it's only possible if you have not triggered the collection loading yet (in case it's lazy-loaded).

If already loaded, you're forced to redo your query.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:14 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
Thanks for your reply. I think I did not describe my question clear.

I mean that I first load the collection, then I save/delete some object, but I did not insert/remove the object from the collection.

For example, for save(object), after I execute the save(), and flush(). At this time, the table in database has been inserted for this object. However, the collection in memory has not such object in it.

I want to know except I reload the entire the collection, is there any other way to refresh the collection? since if I just use load(class, objectID), I can just load the object and it is not in the collection ( I must insert it into the list manually).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:19 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
zhb402 wrote:
Thanks for your reply. I think I did not describe my question clear.

I mean that I first load the collection, then I save/delete some object, but I did not insert/remove the object from the collection.

For example, for save(object), after I execute the save(), and flush(). At this time, the table in database has been inserted for this object. However, the collection in memory has not such object in it.

I want to know except I reload the entire the collection, is there any other way to refresh the collection? since if I just use load(class, objectID), I can just load the object and it is not in the collection ( I must insert it into the list manually).


http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#refresh(java.lang.Object)

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:42 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
Quote:
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#refresh(java.lang.Object)


This is not what I want.
Use refresh(), it can not read the specific object from database into memory. However, in the collection (I read into memory before), which is still does not contain the new inserted object. I want some way to read this new object into the collection, that means refresh the collection. I can not refresh the collection directly, like refresh(collection)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:52 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
zhb402 wrote:
Quote:
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#refresh(java.lang.Object)


This is not what I want.
Use refresh(), it can not read the specific object from database into memory. However, in the collection (I read into memory before), which is still does not contain the new inserted object. I want some way to read this new object into the collection, that means refresh the collection. I can not refresh the collection directly, like refresh(collection)


refeshing the object would refresh the collection on it ..( check ur cascade mapping )

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 2:05 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
scarface wrote:
refeshing the object would refresh the collection on it ..( check ur cascade mapping )


I think you misunderstand me. The collection is not an component of the object. It is a collection that includes the object. For example, I have a Students class, the collection is the query result of all the students. Once I add a new student instance into database, the new student does not appear in the collection of students, even I refresh the new student instance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 2:36 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
So, if I sum up.

You do something like that :

Code:
List<Student> students = session.createQuery("from Student").list();
Student newStudent = new Student(parameters...);
session.save(newStudent);

//newStudent is not in the "students" list

session.refresh(newStudent);
//Here, you would expect to find newStudent in the "students"

If this is it, sorry but it's impossible. You'll have to rerun the first query :-).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 2:46 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
batmat wrote:
So, if I sum up.

You do something like that :

Code:
List<Student> students = session.createQuery("from Student").list();
Student newStudent = new Student(parameters...);
session.save(newStudent);

//newStudent is not in the "students" list

session.refresh(newStudent);
//Here, you would expect to find newStudent in the "students"

If this is it, sorry but it's impossible. You'll have to rerun the first query :-).


Yes. This is waht I want.


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