-->
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.  [ 25 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Session.load: HibernateException or to return null?
PostPosted: Thu Aug 28, 2003 4:01 pm 
Newbie

Joined: Thu Aug 28, 2003 3:58 pm
Posts: 19
Session.load() raises an exception if the bean can't be find by its id. Is there any way to return null instead of throwing an exception. I want to use this for my testCases (and in future implementations when I want to take some action if an entity cant be found).

See my test case:

----------------------------------------------
//Checks that the user was correctly inserted
Long result = addUser(user);
User expectedResult = loadUser(result);
assertEquals(user.fields(), expectedResult.fields());

//Now tests the exclusion
removeUser(result);

//If I try to load the user, it does not exists anymore so null should be returned
assertNul(loaduser(result));
----------------------------------------------

By throwing an exception, Hibernate makes it dificult to know if the user doesn't exists or if another internal error (a lost of conection with the DB for example) happened.

Null in my case should be a valid result?
Or is there another aproach to what I'm trying to do?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 4:03 pm 
Newbie

Joined: Thu Aug 28, 2003 3:58 pm
Posts: 19
And where is the link to edit my messages. Lots of spelling and grammar errors on the one above.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 4:13 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:55 am
Posts: 28
Location: Dallas, TX
I have a class that acts as a facade to all Hibernate APIs. My load method look similar to what you suggested:

Code:
  public Object load(Class clazz, Serializable id) {
       
    Session session = getSession();
       
    try {

      try {
        return session.load(clazz, id);
      }
      catch (ObjectNotFoundException e) {
        // trap ObjectNotFoundException and return null instead. this is
        // the expected behavior for most methods that use this method
        LOGGER.info(e.getMessage());
        return null;
      }
    ...
  }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 4:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No! You should not do this. Because load() might return a proxy, and not hit the db at all.


You should do:


Code:
(Foo) session.createQuery("from Foo foo where foo.id=?) .setParemeter(id).result();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 4:51 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:55 am
Posts: 28
Location: Dallas, TX
Thanks for the recommendation. Sorry for pointing people in the wrong direction. I have never used proxies, so this has never been a problem (yet).

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 5:35 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:55 am
Posts: 28
Location: Dallas, TX
Just to clarify...

The code posted does not seem to jive with the APIs. Would this be a recommended approach:

Code:
Iterator iter = session.createQuery("from Foo foo where foo.id=?").setParameter(1, id).iterate();
return (Foo) iter.hasNext() ? iter.next() : null;


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 5:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
yes, of course, i did some misprints


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 5:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Oh, and also result() does not exist in 2.0. (Its in 2.1)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 4:43 am 
Newbie

Joined: Fri Aug 29, 2003 4:36 am
Posts: 16
Location: Belgium
breidenr wrote:
I have a class that acts as a facade to all Hibernate APIs. My load method look similar to what you suggested:

Code:
  public Object load(Class clazz, Serializable id) {
       
    Session session = getSession();
       
    try {

      try {
        return session.load(clazz, id);
      }
      catch (ObjectNotFoundException e) {
        // trap ObjectNotFoundException and return null instead. this is
        // the expected behavior for most methods that use this method
        LOGGER.info(e.getMessage());
        return null;
      }
    ...
  }


Quote:
No! You should not do this. Because load() might return a proxy, and not hit the db at all.


I'm a newbie so sorry for the question, but can someone explain this a little further, I still don't get why the code above is not such a good idea.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 7:33 am 
Newbie

Joined: Thu Aug 28, 2003 3:58 pm
Posts: 19
I ask the same thing. What about the proxie stuff?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 7:38 am 
Newbie

Joined: Thu Aug 28, 2003 3:58 pm
Posts: 19
Also, I have a method to load any kind of objects so I need to use load().

That's because if I use queries I need to know what object I want to load and then its name/properties can vary quite a bit. Also I do not want to pass Strings to complete the query string.

Code:
publiv Object loadObject(String name, String idProperty, Long value) {
  ...
  session.find("from " + name + " where " + idProperty + " = ?"...
}


Ugly don't u think? Is there any other aproach instead?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 8:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
freitasoo wrote:
Also, I have a method to load any kind of objects so I need to use load().

That's because if I use queries I need to know what object I want to load and then its name/properties can vary quite a bit. Also I do not want to pass Strings to complete the query string.

Code:
publiv Object loadObject(String name, String idProperty, Long value) {
  ...
  session.find("from " + name + " where " + idProperty + " = ?"...
}


Ugly don't u think? Is there any other aproach instead?


Dude! .id is a keyword in hql! So the code should just be:

Code:
public Object loadObject(Class class, Object idvalue) {
  ...
  session.find("from " + class.getName() + " where id = ?", idvalue);
}

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 2:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually:

Code:
public Object loadObject(Class class, Object idvalue)  {
  return session.find("from " + class.getName() + " x where x.id = ?", idvalue);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 3:26 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
How to do that with a compound id? I tried this:
Code:
List list = session.find("from " + stateName + " x where x.id = ?", identity, Hibernate.SERIALIZABLE);

and
Code:
List list = session.find("from " + stateName + " x where x.id = ?", identity, Hibernate.serializable(identity.getClass()));

but I'm getting
Code:
net.sf.hibernate.QueryException: path expression ends in a composite value: category0_.id [from com.*****.cc.objectlib.category.CategoryState x where x.id = ?]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 4:00 pm 
Newbie

Joined: Thu Aug 28, 2003 3:58 pm
Posts: 19
Oh (blush)!! I didn't know id was a keyword. What a dumbass!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 25 posts ]  Go to page 1, 2  Next

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.