将Hibernate结果转换为对象列表 [英] Cast Hibernate result to a list of objects

查看:88
本文介绍了将Hibernate结果转换为对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 列表<关联>我的DAO中有一个hibernate调用, ());} =(List&Associate>)List.createSQLQuery(SELECT * FROM associates WHERE fk_id =:id AND fk_associate_id =(SELECT id FROM users WHERE fk_user_type = 2))setParameter(id,id).list() ; 

我收到一个错误,说我不能将结果列表转换为模型类型Associate。我不明白为什么会发生这种情况。我只返回联系人表中的字段。

解决方案

您需要指定结果应该是的实体类转换为使用 addEntity(),因为您正在执行不知道实体的SQL查询:

$ p> 列表与LT;准> associate =(List< Associate>)session.createSQLQuery(
SELECT * FROM associates WHERE fk_id =:id AND fk_associate_id =(SELECT id FROM users WHERE fk_user_type = 2))
.addEntity(Associate。 class)
.setParameter(id,id).list();

另请参阅:




I have a hibernate call in my DAO that looks like this

List<Associate> associate = (List<Associate>)session.createSQLQuery("SELECT * FROM associates WHERE fk_id = :id AND fk_associate_id = (SELECT id FROM users WHERE fk_user_type = 2)").setParameter("id", id).list();

I am getting an error saying that I cannot cast the resulting list to the model type Associate. I don't understand why this is happening. I am returning only the fields that are in the associates table.

解决方案

You need to specify the class of entity the result should be converted to using addEntity(), because you are executing SQL query that doesn't know anything about entities:

List<Associate> associate = (List<Associate>) session.createSQLQuery(
    "SELECT * FROM associates WHERE fk_id = :id AND fk_associate_id = (SELECT id FROM users WHERE fk_user_type = 2)")
    .addEntity(Associate.class)
    .setParameter("id", id).list(); 

See also:

这篇关于将Hibernate结果转换为对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆