-->
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: utility method to check if an object is transient/detached
PostPosted: Mon Nov 15, 2004 9:29 pm 
Beginner
Beginner

Joined: Mon Nov 15, 2004 8:32 pm
Posts: 36
Hi,
Is there any utiliity method in hibernate which can quickly determines if an object is transient or detached (similar to JDO's PersistenceManager.isTransient(), etc).
I know you can use an object's id to determine it, but it requires some knowledge of the object (i.e. access the id field or the POJO's getID() method). What I am looking for is some utility method which takes an object and check if it's transient or detached without any knowledge of the object.
We are trying to port our JDO app to Hibernate. That's why I am asking this feature.


Thanks,
--Jiunjiun

[b]Hibernate version:[/b]
2.1.4

[b]Mapping documents:[/b]
N/A

[b]Code between sessionFactory.openSession() and session.close():[/b]
N/A

[b]Full stack trace of any exception that occurs:[/b]
N/A

[b]Name and version of the database you are using:[/b]
Postgres 7.3.x

[b]The generated SQL (show_sql=true):[/b]
N/A

[b]Debug level Hibernate log excerpt:[/b]
N/A


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 6:33 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why would you need to make this decision? Transparent persistence should be transparent to the rest of your application and it shouldn't matter if you work with an object in detached or transient state.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 4:02 pm 
Beginner
Beginner

Joined: Mon Nov 15, 2004 8:32 pm
Posts: 36
As I mentioned, I was porting our app from JDO. In our current app code, if we got a new object from client side, we would did some clean up to the persistent store to remove any possible left over instance (happens if the app crashed). JDO has those utility methods to check the life-cycle state of an object and we are using it.
Also, I think those methods will be useful. True, in a ideal transparent persistence world, you won't need to worry about them. But in reality, sometimes you just have to. For example, the result of saveOrUpdateCopy() will return you the same object, or a persistent copy, depends on whether you passed a transient/detached instance. If such utility methods exists, I will be more certain about the behavior of my saveOrUpdateCopy. I may even do something special if a new instance is created (sending an event is one example).
In general, I think if the object life cycle states (transient->persistent->detached) is specifically defined in hibernate. It will be convenient to have some utility methods to check it.

Thanks,
--Jiunjiun


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 4:03 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Session.contains().

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 7:35 pm 
Beginner
Beginner

Joined: Mon Nov 15, 2004 8:32 pm
Posts: 36
Session.contains() only tells you if an object is associated with the session, not it's transient or detached. It returns the same value if your object (transient or detached) is associated/disassociated with the session.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 7:36 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
if (obj.getId != null) return DETACHED;

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 7:46 pm 
Beginner
Beginner

Joined: Mon Nov 15, 2004 8:32 pm
Posts: 36
To do it, you have to be able to cast the object to a certain class and have some knowledge of the id field (or access method depend on the mapping). What I get is a generic Object. It could be any persistent object and I can't make any assumption about downcasting it. Some kind of introspection has to be done here and I think it's much easier for hibernate to provide that functionality since it has all the mapping info.

Thanks,
--Jiunjiun


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 5:06 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Well, its not there, so someone would have to write it.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 3:54 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
I have the same need. I have to know whether an object passed to one of my business delegates is detached or transient so I can attach it to the session (using lock) if and only if it is not transient. Since I don't know how the caller got their hands on the object, I can't assume its state. Seems like a legit use case.

I can call lock and catch the exception it throws if the object is transient, but that seems like a hack.

Add it to the wish list, I guess. :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 9:29 am 
Newbie

Joined: Fri Dec 03, 2004 5:57 am
Posts: 1
I guess you could do something like this:

Code:
ClassMetadata tClassMetadata = HibernateUtil.getSessionFactory().getClassMetadata(pEntity.getClass());

if(tClassMetadata.getIdentifier(pEntity)!= null) return DETACHED


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.