-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem checking a record by ID
PostPosted: Fri Jun 05, 2009 3:22 am 
Newbie

Joined: Tue Apr 14, 2009 4:40 am
Posts: 5
Please, help me.
In a java-hibernate query I have to test if exist a record with a ID (primary key), without throwing an Exception if the record doesn't exist.
I've tried this code:
if(theClassThatMakeTheQuery.getRecordById(testId)!=null)){
...
}
but it does'nt work because when the record with the testID doesn't exist, it throws an exception.
I need a true/false result or anything else but an exception
The stack trace:

[INFO] DefaultLoadEventListener - Error performing load command <org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pk.bjects.MyObjectObj#1156]>org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pk.bjects.MyObjectObj#1156]
at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:128)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:770)


Thanks in advance


Top
 Profile  
 
 Post subject: Re: Problem checking a record by ID
PostPosted: Fri Jun 05, 2009 8:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Hi,
please attach getRecordById code.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Re: Problem checking a record by ID
PostPosted: Fri Jun 05, 2009 8:46 am 
Beginner
Beginner

Joined: Mon Jun 01, 2009 5:39 am
Posts: 34
Why don't you just:

Code:
boolean found = true;

try {
    theClassThatMakeTheQuery.getRecordById(testId);
} catch (org.hibernate.ObjectNotFoundException e) {
    found = false;
}

if (found) {
    // proceed
}


Top
 Profile  
 
 Post subject: Re: Problem checking a record by ID
PostPosted: Mon Jun 08, 2009 4:47 am 
Newbie

Joined: Tue Apr 14, 2009 4:40 am
Posts: 5
Yes !
Thank you very much, fge.
This code is perfect for what I was trying to do.
Thank you very much.


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