-->
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.  [ 4 posts ] 
Author Message
 Post subject: Find out the state of an object (transient or detached)
PostPosted: Tue Mar 10, 2009 6:06 am 
Beginner
Beginner

Joined: Fri Sep 26, 2008 2:39 am
Posts: 20
Is there a simple way to find out the state of a given object?

I need to know if it's a new object (transient) or if it is detached.

Because if it is detached lazy loading of a collection will fail. - So before accessing the collection i have to session.update() the object.

But if it is transient session.update() will fail.

Right now i try to access the collection, catch the lazy loading exception and in the catch block i reattach the object to the session.


What I am looking for is a isTransient(Object object) or isDetached(Object object) method or a getState(Object object) to tell me what the actual state is.

(The Hibernate Session seems to have no such method.)

Thanks in advance,
jp


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 7:09 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
JP-Ulm wrote:
Right now i try to access the collection, catch the lazy loading exception and in the catch block i reattach the object to the session.

I think what you are doing now is the right way to do it.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 4:47 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
I am not clear with your problem.

Can you use session.saveOrUpdate(object) ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 4:58 am 
Beginner
Beginner

Joined: Fri Sep 26, 2008 2:39 am
Posts: 20
SIau_Tie wrote:
I am not clearly with your problem.

Can you use session.saveOrUpdate(object) ?


It is a little more complex than I described above.

The thing is, I have to access the lazy-loading set without saving any changes that might have been done to the object since it was loaded to the database.

(The Object is opened in the application in an edit form, which has a save button - until that button is pressed the object can not be written to the db, because the user could still just close the form without saving)

Now while the object is displayed in this form, the user may choose to open a tool on a sidebar of the application - this tool now has to access the lazy-set.

This tool now would like to know if the object is detached, transient or persistet to choose how to access the collection correctly.

Since I could not get that state what I do now is:

I check if the object id is not null (tells difference between transient & detached/persistet ) - if it is not null (=not transient) I load the set manually using the id, the class and a hql query like this "select mySet from class where id = id" and then attach the loaded set to the object. (object.setMySet(justLoadedFromDBSet) )- then i can access and modify the set and not care if the object ist attached to a session or not.

If the user chooses to press the save button the object is then saved with saveOrUpdate and the set is mapped with cascade="all" and all works out.

This is the solution for me.

So I think this closes this thread. - sorry for not beeing clear enough in the first post, I oversimplyfied the problem too far I guess.


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