Search code examples
javahibernatehql

Return Map from hibernate


I am trying to create a Map object from the HQL

Here is my code

I am facing the exception :

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long

List<Map<Long, Long>> test = new ArrayList<Map<Long, Long>>();
try {
    String HQL_QUERY = "select new map(hp.col1, hp.col2) from HP hp where hp.col1 in (:id)";
    test = getSession().createQuery(HQL_QUERY).setParameter("id", id).list();
}catch(Exception e){

}

No idea where i am making mistake

Please help


Solution

  • I made one mistake

    instead of parameter i need to use parameterlist.

    Here is the code :

     List<Map<Long, Long>> test = new ArrayList<Map<Long, Long>>();
    try {
        String HQL_QUERY = "select new map(hp.col1, hp.col2) from HP hp where hp.col1 in (:id)";
        test = getSession().createQuery(HQL_QUERY).setParameter("id", id).list();
    }catch(Exception e){
    
    }