-->
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.  [ 3 posts ] 
Author Message
 Post subject: Exceptions in session.load
PostPosted: Wed Jul 19, 2006 7:12 am 
Newbie

Joined: Wed Jul 19, 2006 6:49 am
Posts: 1
Hi All,

Session.load throws ObjectNotFoundException when it fails to select a row with a given primary key.So to avoid that can we go for session.get which returns a null value when it fails to select a row.In which scenario can we go for session.load?What are the advantages for session.load?

Thanks,
Sajan.C


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 7:34 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Use load when you are sure that the object with the given identifier should exist. If it does not, then it would be an actual error. If you are not sure then use get.


More about it in :

http://www.hibernate.org/hib_docs/v3/re ... te-loading


Top
 Profile  
 
 Post subject: Re: Exceptions in session.load
PostPosted: Wed Jul 19, 2006 7:49 am 
Beginner
Beginner

Joined: Wed Aug 10, 2005 12:09 am
Posts: 30
sajan wrote:
Hi All,

Session.load throws ObjectNotFoundException when it fails to select a row with a given primary key.So to avoid that can we go for session.get which returns a null value when it fails to select a row.


yes,

Code:
protected Entity getEntity(Id id) {
  return session.get(Entity.class, id);
}

public Entity findEntity(Id id) throws NotFoundException {
  Entity e = getEntity(id);
  if (e == null) throw new NotFoundException();  // checked exception
  return e;
}



sajan wrote:
In which scenario can we go for session.load?What are the advantages for session.load?


use load() when you're doing something and just want an object and not worrying about null or exception handling. If theres exception you probably want to rollback anyway...


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