0
final List <Map<String , Object>>  roleList;
final Map<Integer, String> roleMap=new HashMap<Integer, String>();

roleList = getSession()
    .createQuery("select Id, Name from Emp where dept=:ref")
    .setParameter("ref", "accounts")
    .list();

for (Map<String, Object> map2 : roleList) 
{
    roleMap.put((Integer)(map2.get("Id")), (String)map2.get("Name"));
}

MappingBean.setRoleList(roleMap);

The above code is showing class cast exception [Ljava.lang.Object; cannot be cast to java.util.Map. Is there any way in Hibernate we can get data in the form of List of Maps? My roleList is in the form of map and I want to set the roleList.

3
  • 1
    What type exactly has your roleList? Commented Oct 15, 2013 at 13:53
  • take a look at Criteria.ALIAS_TO_ENTITY_MAP Commented Oct 15, 2013 at 14:04
  • @EelLee it is of type Map of String and Object Commented Oct 17, 2013 at 13:09

2 Answers 2

5

When you make a select like that:

.createQuery("select Id, Name from Emp where dept=:ref")

Hibernate returns by default a List<Object[]>, because that's the safest way to represent a given subset of fields that by no means are bound to have the same type (hence, the lowest common class is considered Object).

You can, however and given the case, transform the result into a map with a simple iteration or through the use of configurations. IMO it seems like an excercise in futility because each record will be returned in a key - Value fashioned way already (even if it is represented as an array of two objects).

Avoid yourself such overhead by doing this, directly (change the type of roleList):

for (Object[] role : roleList) 
{
    roleMap.put((Integer)role[0]), (String)role[1]);
}

The Object[] returned by Hibernate respects the order of the selected fields so, in your case, index 0 corresponds to Id and 1 to Name.

Sign up to request clarification or add additional context in comments.

2 Comments

final List <Map<String , Object>> roleList My list is of type Map so y didnt it give me error at first place
@AnkitDuggal Because you created roleList but you didn't assign it a value right away. The ClassCastException was thrown while trying to assign the List<Object[]> returned by Hibernate to your List<Map<String,Object>>. The error clrearly states that the cast to Map failed.
0

You could add a ResultTransformer to your query to accomplish this. I am not sure if there is one that does what you want out of the box, though. You may need to write your own. It looks fairly simple, though. I think all you would need would be to extend BasicTransformerAdapter and override the transformTuple method to return a map. This should allow you to control the value returned from query.list() so you can set it directly on MappingBean.

2 Comments

what is result transformer?
It is a class provided by Hibernate that can be used to transform the result sets that are returned by Hibernate queries. It lets you mish-mash into whatever format you want returned. See the javadoc for org.hibernate.transform.ResultTransformer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.