Return 来自休眠的地图

Return Map from hibernate

我正在尝试从 HQL 创建地图对象

这是我的代码

我遇到了异常:

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){

}

不知道我哪里弄错了

请帮忙

hp.col1hp.col1 返回 java.util.ArrayList,应该是 Long。在实体 class.

中更改它的类型

更改测试类型: List<Map<List, Long>> test = new ArrayList<Map<List, Long>>();

HQL 就是这样工作的:

List<MyObject> test = new ArrayList<>();
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).getResultlist();
}

所以你应该改变你的属性类型,然后你可以编写一些代码将你的列表转换为地图

我犯了一个错误

我需要使用参数列表而不是参数。

这是代码:

 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){

}