简体   繁体   中英

ClassCastException in hibernate with unknown source of HashMap

I'm currently stuck on ClassCastException where I cant figure out whats happening exactly.

NOTE the query itself has been altered for simplicity, as I believe the problem lies elsewhere.

Situation goes as follows: I want to retrieve a list of database entries in my service class with:

List<Object[]> queryList = dao.queryGenericArray("SELECT a, b FROM db");

In my understanding, the method queryGenericArray should return a list of object arrays, as defined in the DAO class:

List<Object[]> result = q.list();

list() being defined by hibernate.

So now I have my queryList object array without any errors, so far so good. But when I try to access an array from the queryList, i get a ClassCastException stating:

java.lang.ClassCastException: java.util.HashMap cannot be cast to [Ljava.lang.Object;

My question now is, why and where is the list entry defined as HashMap ?

Thanks in advance, if I left out any vital information, please let me know.

Stacktrace

Ok, I figured it out, thanks to the comments on my inital question.

Problem was, I oversaw (or neglected) one line of code in the DAO class (I am not the author of this class):

q.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

This line, transforms the object[] to a map, as pretty concisely stated.

What I further had to do, was for one change the return type of my queryGenericArray method from object[] to List<HashMap<String, Date>> and secondly cast the result of the hibernate method like this:

List<HashMap<String, Date>> result = q.list();

Now I can correctly access the data in the hashmaps contained in the passed list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM