-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to check if object already stored in database ?
PostPosted: Wed Feb 04, 2004 11:49 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
How can I check if an object is already persisted in the database ?
I want to do something like

Object obj ;
Session session ;

if ( ! obj.persistedInDatabase() )
{
session.save(obj);
}

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 12:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Probably take a look at saveOrUpdate() it might be just what you are trying to do. Consider if you can check the saved-state using a property, eg. test for id==null or something. Otherwise try to load the object.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 12:18 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
Maybe it seems strange, but if the object is alerady persisted I don't want to update it. So therefore I assume the method saveOrUpdate is not the right one to use.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 12:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well so consider the other possiblitities. If you can check for id==null or something. Otherwise try to load the object from the db and see if you find it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 12:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use session.get()

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 12:35 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
Thanks, I am using that. The "problem" is just that I have wrong version of Hibernate where the 2 get-methods in the API are not present in my version of Hibernate.

But anyway, thanks for the help !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 1:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can still use find with an appropriate query.


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