-->
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: When is ObjectNotFoundException thrown in hibernate
PostPosted: Wed May 06, 2009 10:51 am 
Newbie

Joined: Fri Jan 16, 2009 8:40 am
Posts: 7
Hi,

I have a requirement wherein i need to query database via session.load() or session.get() & if no matching row exists , want to throw runtime exception.
I guess session.load() throws ObjectNotFoundException .Does session.get() throws this exception .
Also please suggest, in case i get HibernateException, which is a runtime exception,how can i show meaningful message to the user.

Thanks in advance

Nisha


Top
 Profile  
 
 Post subject: Re: When is ObjectNotFoundException thrown in hibernate
PostPosted: Wed May 06, 2009 12:22 pm 
Beginner
Beginner

Joined: Wed Jan 25, 2006 10:18 am
Posts: 28
session#get(...) does not throw an exception if there's no matching row, it returns null instead.

session#load(...) always returns an object and does not throw an exception until you try to invoke a method on that object.

How you show a meaningful message to your user in case no matching row is found is obviously completely specific to your application.
I would use get(...), then check for null, and if it's null then show a message.


Top
 Profile  
 
 Post subject: Re: When is ObjectNotFoundException thrown in hibernate
PostPosted: Thu Jan 07, 2010 6:27 am 
Beginner
Beginner

Joined: Thu Jul 06, 2006 11:09 pm
Posts: 25
I totally disagree with this. I just encountered ONFE when using the Session.get() method.

Here is the main stack trace:
Code:
Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [entity.PkgGen#PkgGenId[trackNum=123,mpsNum=1]]
        at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:419)
        at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:171)
        at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
        at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906)
        at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:874)
        at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
        at org.hibernate.type.EntityType.resolve(EntityType.java:412)
        at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:139)
        at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
        at org.hibernate.loader.Loader.doQuery(Loader.java:752)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
        at org.hibernate.loader.Loader.loadEntity(Loader.java:1885)
        at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
        at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
        at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3062)
        at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
        at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
        at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
        at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
        at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:843)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:836)
        at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:531)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
        ... 52 more

_________________
Don't forget to rate.


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.