2

I'd like to write a generic method in JPA 2 that can load all entities of class T if it is given the collection of their primary keys. Here is the signature of the desired method:

public <T> List<T> getEntityList(final Class<T> entityClass, final Collection<Object> primaryKeys)

Is it possible somehow even if I don't know the entity class nor the primary key class?

jabal
  • 11,987
  • 12
  • 51
  • 99
Andrea T
  • 3,035
  • 4
  • 23
  • 39
  • Have a look here: http://stackoverflow.com/a/11349052/870122 and http://stackoverflow.com/a/9325205/870122 – perissf Oct 18 '12 at 13:36

1 Answers1

-1
public <T> List<T> getEntityList(final Class<T> entityClass, final Collection<Object> primaryKeys)
    final Session session = sessionFactory.getCurrentSession();
    final Criteria crit = session.createCriteria(entityClass);
    crit.add(Restrictions.in("id", primaryKeys);
return crit.list();

Using a criteria query and hibernate makes it pretty simple. The JPA way is pretty similar I think.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311