Reputation: 21245
I have a user table, how do I create a Criteria
in Hibernate that returns a Map with the UserId as the key and the User object as the value?
Upvotes: 2
Views: 4713
Reputation: 909
You can use HQL , which has
new map
to return the required data in the form of map. If you can use this, then I am happy to share further details on how to do this.
Upvotes: 0
Reputation: 862
Using Google guava
ImmutableMap<Long, User> immutableMap = Maps.<Long, User>uniqueIndex(hibernateSession.createCriteria(User.class).list(),
new Function<User, Long>() {
public Long apply(User input) {
return input.getId();
}
});
Upvotes: 0
Reputation: 81907
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.
Upvotes: 1