-->
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.  [ 13 posts ] 
Author Message
 Post subject: Which method of Session should I choose?
PostPosted: Wed Nov 16, 2005 11:15 pm 
Newbie

Joined: Thu Nov 10, 2005 11:05 pm
Posts: 4
Hi,
I have an object deserialized from XML ( through XStream ), which method of Session should I use to save the object to DB? Before I save it, neither do I know if there is an instance of this object associated with the session, nor if there is a record with the same id in db. What can I do to handle this situation?
Thanks a lot!


Top
 Profile  
 
 Post subject: 2c
PostPosted: Wed Nov 16, 2005 11:23 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
If mapping specifies unsaved value for object id (null) then session.saveOrUpate( object ) will insert or update record depending on value of ID of the deserialized object.
Otherwise you need to search DB for an object and if it is present then session.merge( object ) , or session.save(object) if not.
Depending on mapping you may need to assign identifier ( when generator=”assigned”).

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Thanks but problem not wholly solved
PostPosted: Thu Nov 17, 2005 3:07 am 
Newbie

Joined: Thu Nov 10, 2005 11:05 pm
Posts: 4
The object has a composite id, so specify unsaved-value to null is invalid. And how can I query the presence of the record with the same id as this object? Hibernate3's load method requires an Identifier which must be obtained from a persitent object.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 3:12 am 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
Composite IDs are bad in general. In this case they are even more bad.

Every row in a table should have a unique ID number. Why fight it?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 3:26 am 
Newbie

Joined: Thu Nov 10, 2005 11:05 pm
Posts: 4
Legacy db always exists. There's no reason or need to force legacy system to think in hibernate's way.


Top
 Profile  
 
 Post subject: find
PostPosted: Thu Nov 17, 2005 12:03 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
to find out if there is such object do:
q = session.createQuery( "from YourClass yc where yc.id.part1= :p1 and yc.id.part2=:p2");
q.set( "p1" ....
if( q.list().size() == 0 ){
insert new (save(x) )
}else{
update ( session.merge(x))
}

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 3:07 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
or simple call
Code:
try {
  session.load(YourObject.class, yourObject);
  exists=true;
} catch (HibernateException e) {
  exists=false;
}


Top
 Profile  
 
 Post subject: Exeptions
PostPosted: Thu Nov 17, 2005 3:38 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
IMO application logic must not rely on Exception for normal course of operations.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: Exeptions
PostPosted: Thu Nov 17, 2005 4:04 pm 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
kgignatyev wrote:
IMO application logic must not rely on Exception for normal course of operations.


I agree. Exceptions are for exceptional events.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 4:14 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
then
Code:
if (yourObject!=null && session.get(YourClass.class,yourObject) == null)
  session.save(object);
else
  session.merge(object);


creating query for composite id is pain


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 4:16 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
really
Code:
if (yourObject!=null) {
  if (session.get(YourClass.class,yourObject) == null)
    session.save(object);
  else
    session.merge(object);
}



Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:40 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
snpesnpe wrote:
or simple call
Code:
try {
  session.load(YourObject.class, yourObject);
  exists=true;
} catch (HibernateException e) {
  exists=false;
}


Why not use get?
Then you can check for null instead of having to catch an exception.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 5:32 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
hhuber wrote:
snpesnpe wrote:
or simple call
Code:
try {
  session.load(YourObject.class, yourObject);
  exists=true;
} catch (HibernateException e) {
  exists=false;
}


Why not use get?
Then you can check for null instead of having to catch an exception.


Already answered by later posts.

Sorry,
Heinz


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