Hibernate criteria to return result in a map

how to return map<key, value> with jpa
how to return map<key, value> with hql
how to return map<key, value> with sql
hibernate native query return map
jpa query map key value
return map from hibernate
return map from jpa repository
hibernate result set mapping

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?

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.

How to return Map<Key, Value> with Hibernate\HQL, Learn how you can return a Java Map result when executing a JPA query using either getResultStream or Hibernate ResultTransformer. the result set of a given JPQ query, be it JPQL, Criteria API or native SQL query. If your query returns all columns that are mapped by an entity, you can tell Hibernate to map the result to a managed entity. Afterwards, you can use the entity in the same way as any other entity.

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();

How to return a Map result from a JPA or Hibernate query, We can use Hibernate Criteria to convert records returned by Hibernate to a Map. For this, we can set the ResultTransformers in following� If you're not actually mapping it in this way but want to create a map using either HQL or a Criteria query, you can create a ResultTransformer to create a map from the returned result set. Judging from Xavi's answer, I guess there is also support in HQL for creating a map without using a transformer.

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.

Transforming database query result to Map using Transformers and , Narrowing the result set You can order the results using org.hibernate.criterion. Order . The second createCriteria() returns a new instance of Criteria that refers to the elements of the kittens collection. Cat cat = (Cat) map.get(Criteria. Result Set Mapping: Hibernate Specific Features In the first post , we had a look at some basic mapping definitions to map the query result to an entity. The mapping definitions got more complex in the second part , as we mapped the query result to multiple entities and handled additional columns.

Chapter 17. Criteria Queries, Hibernate features an intuitive, extensible criteria query API. Cat cat = (Cat) map.get(Criteria. This is useful when there is a need to order or limit in the database prior to returning complex/large result sets, and removes many instances� The query needs to return all properties of the entity and the JPA implementation (e.g. Hibernate) will try to map the returned columns to the entity properties based on their name and type. If that is successful, the EntityManager will return a list of fully initialized Author entities that are managed by the current persistence context.

Chapter 16. Criteria Queries, Learn how to map query results into custom objects with Hibernate. in the select clause. But, when we do this, Hibernate returns a list of arrays instead of a list of Objects: Now, let's look at the HQL that uses this class:� import java.util.Date; public class Survey { private int id; private String name; private Date purchaseDate; public Date getPurchaseDate() { return purchaseDate; } public void setPurchaseDate(Date purchaseDate) { this.purchaseDate = purchaseDate; } public String getName() { return name; } public void setName(String name) { this.name = name

Mapping A Hibernate Query To A Custom Class, Hibernate - Criteria Queries - Hibernate provides alternate ways of of a criteria query is one, which will simply return every object that corresponds to the Employee class. Criteria cr = session.createCriteria(Employee.class); List results = cr.list(); <hibernate-mapping> <class name = "Employee" table = " EMPLOYEE">� For handling query results, we are using setResultTransformer( Criteria.ALIAS_TO_ENTITY_MAP ) method which converts each row of result into a Map. Here is the video tutorial of: "ALIAS_TO_ENTITY_MAP in Hibernate"

  • Awesome to know! But I was looking for a solution that doesn't use another library.
  • I know what you mean. I use Guava for loads of stuff so it adds no additional dependencies.
  • well tbh, either way it will go that route. cuz either hibernate returns a map or you convert to map. once you centralised the map conversion there is no big diff at all.