-->
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: Get list by primary keys (plural) - Generic DAO
PostPosted: Tue Apr 13, 2010 10:54 am 
Newbie

Joined: Tue Apr 13, 2010 10:46 am
Posts: 1
I'm trying to create a generic dao class that contains a function which returns a list of entities based off a list of primary keys, and I can't figure out how to do this in hibernate.

The usual way of getting an element by primary key would be:

Code:
    public T get(PK id) {
...
return session.load(entityClass, id);
}


But I want this on a list level.

Code:
    public List<T> getListByPrimaryKey(List<PK> keys) {
      // how do I implement this?..

}



Any ideas?


Top
 Profile  
 
 Post subject: Re: Get list by primary keys (plural) - Generic DAO
PostPosted: Tue Apr 13, 2010 11:10 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
It's something like: session.createCriteria(Class).getlist();


Top
 Profile  
 
 Post subject: Re: Get list by primary keys (plural) - Generic DAO
PostPosted: Wed Apr 14, 2010 6:19 am 
Newbie

Joined: Tue Apr 06, 2010 8:46 am
Posts: 7
I guess what you need is something like this:

Code:
   public <T> List<T> getListByPrimaryKey(Class<T> clazz, String pkProperty, List<?> keys)
   {
      Session ses = sessionFactory.getCurrentSession();
      return ses.createCriteria(clazz)
               .add(Restrictions.in(pkProperty, keys))
               .list();
   }


Of course if you have a very big list of identifiers there are some limitations in the number of the in-clause parameters you can have (Oracle is 1000, MsSQL is 2100 etc... )
for more info see http://opensource.atlassian.com/projects/hibernate/browse/HHH-1123


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.