0

I've a method that retrieves some data from a table and return it in a java.util.List, I need to call that method many times so I'd like to call it just once and put all the data in a java.util.Map, how can I do this?

My method is actually like to:

private List<?> getAll(final Class objClass, final Integer parentId) {

    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(objClass);
    if (parentId != null) {
        criteria.add(Restrictions.eq("tab.parent", parentId));
    }

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return (List<?>) criteria.list();

}

How can I get a HashMap like HashMap<parentId,List<?>>?

Thanks

Alessandro
  • 4,382
  • 8
  • 36
  • 70

1 Answers1

0

As per stackoverflow answer

I don't think this is possible, cause a query always returns a List: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Criteria.html#list() or in a special case a single element.

There is just no method that could return a map.

So without hacks like overwriting the Session to create a special Criteria which also has a map() method you can't do what you want.

Just return a list and convert it into a map.

Community
  • 1
  • 1
Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45