-->
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.  [ 7 posts ] 
Author Message
 Post subject: get/load to return multiple instances
PostPosted: Fri Jan 14, 2005 6:53 pm 
Newbie

Joined: Fri Jan 14, 2005 6:06 pm
Posts: 3
Does a method like this exist? (returns array of objects instead of single instance)
public Object[] get(Class clazz, Serializable[] ids) throws HibernateException;

I've been hunting around for awhile trying find a quick and easy way to retrieve multiple instances of a class given an array of ids. The idea behind this is fairly straightforward - you have a web page where a user can multi-select elements from a homogenous list. You receive that list from the web page in the form of an array and need to load all of the objects associated to those ids so you can save them to the parent hibernate object. All of the ids are associated to the same class type.

Right now we are just looping through the array of ids and performing a get on each value individually which as you can imagine is not very efficient.

I suppose this could be done with HQL, but then you would have to specify a new clazz and table value in each HQL statement for each object type that could be a child in which case I couldn't easily reuse it.

I was just thinking this is the type of thing most web developers have to deal with so I figured there might be a common solution available?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 10:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Use HQL, not sure what you mean by 'Its not reusable' though. Pass the target class so you can get the class name to build the approrpiate HQL string from clause.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 15, 2005 1:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can also use the criteria api to build such a method yourself very easily


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 16, 2005 12:24 am 
Newbie

Joined: Fri Jan 14, 2005 6:06 pm
Posts: 3
HQL isn't as readily reusable as is since you would need to pass in the class as well as the table name and then do some string concatenation if you want the method to be reusable for any class. Then you need to put the method on a helper class or common superclass (basically it's in a different location than the existing get/load methods on the transaction). It can be done but is just a good bit more awkward than the existing get/load methods which are much more simple in comparison. Like I said I was just wondering if there was an existing API that I could use out of the box. I'll take a look at the criteria API and see what I can come up with.

Not trying to make a big deal out of this, but just like you use the get/load methods when loading a single instance instead of writing an HQL statement or criteria, it would just be nice to be able to pull back multiple instances of the same class using the same style of convenience method since I'm sure this is a VERY common task.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 16, 2005 7:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
As I said, doing it yourself is absolutely trivial:

Code:
public Object[] get(Class clazz, Serializable[] ids) {
     return session.createCriteria(clazz)
           .add(Expression.in("id", ids)
           .list()
           .toArray();
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 16, 2005 7:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I am in the habit of using HQL rather than Criteria API. I am also not so concerned with a little bit of string concatination when and if it is required. However, in this case, Criteria API provides the better solution.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 7:24 pm 
Newbie

Joined: Fri Jan 14, 2005 6:06 pm
Posts: 3
It works great so thanks for the help!

I would still request/suggest that something like this be added to net.sf.hibernate.Session as a standard convenience method similar to get/load convenience methods. I believe this would be a fairly common action for most hibernate users?

But the bottom line is it works fine on my HibernateHelper class and helps cut down on some of the SQL chatter. Thanks again!


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