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...