我的DAO中有一个hibernate的呼叫,看起来像这样
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();
我收到一条错误消息,说我无法将结果列表转换为模型类型“关联”。我不明白为什么会这样。我只返回关联表中的字段。
您需要使用来指定要转换为结果的实体类addEntity(),因为您正在执行对实体一无所知的SQL查询:
addEntity()
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();
也可以看看: