-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to get object state (saved or unsaved)?
PostPosted: Wed Aug 19, 2009 3:25 pm 
Newbie

Joined: Tue Jun 16, 2009 4:11 pm
Posts: 8
Given an object instance and a session, is there any way to find out whether this object is saved or not ***(WITHOUT having to execute a select), can't check if id is null cause the key generation is done before the save command.

*In Ruby's ActiveRecord this would be "object.new_record?".

Any help would really be appreciated;


Top
 Profile  
 
 Post subject: Re: How to get object state (saved or unsaved)?
PostPosted: Wed Aug 19, 2009 9:55 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
What about querying the id and seeing if it is null?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: How to get object state (saved or unsaved)?
PostPosted: Fri Aug 21, 2009 12:26 pm 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
@Cameron McKenzie
Hmmm... But how to check this when @Id has a primitive type, like int or long?

Regards,
Adam Wozniak


Top
 Profile  
 
 Post subject: Re: How to get object state (saved or unsaved)?
PostPosted: Sat Aug 22, 2009 3:24 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Darn, you got me. :(

What's the scenario or purpose for needing this? Is it anything other than the obvious? Maybe if I knew the context of the problem you're trying to solve, it might spark an idea?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: How to get object state (saved or unsaved)?
PostPosted: Sun Aug 23, 2009 3:25 am 
Newbie

Joined: Sat Feb 21, 2009 3:18 am
Posts: 2
Quote:
@Cameron McKenzie
Hmmm... But how to check this when @Id has a primitive type, like int or long?

Regards,
Adam Wozniak

Hi,
in case of primitive types , you can compare it with "0",if Id equals to zero or not.


Top
 Profile  
 
 Post subject: Re: How to get object state (saved or unsaved)?
PostPosted: Fri Aug 28, 2009 9:06 pm 
Newbie

Joined: Tue Jun 16, 2009 4:11 pm
Posts: 8
Hi again, my Id gets set before saving the object, due to a diferent key generation policy, I already solved this using AspectJ, but if by any chances this can be solved using only Hibernate, would be much better. Querying the database before saving is not an option since it may be a very time consuming operation.

Thanks for all the replies.


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