java.lang.ClassCastException:org.hibernate.internal.SQLQueryImpl无法强制转换为java.util.List如何修复

时间:2013-08-09 14:00:49

标签: java sql hibernate exception

 List<Candidate> candidates = (List<Candidate>) session.createSQLQuery("select candidate.* from candidate inner join candidate_skill on candidate.id = candidate_skill.candidate_id inner join skill on candidate_skill.skill_id = skill.id where skill.id = 1");       

我明白了:

 java.lang.ClassCastException: org.hibernate.internal.SQLQueryImpl cannot be cast to java.util.List

查询是正确的。如何解决?

2 个答案:

答案 0 :(得分:12)

您在查询结束时忘记了.list()

应该是

................skill.id where skill.id = 1").list();

有关详细信息,请参阅hibernate documentation

答案 1 :(得分:2)

查询不返回任何内容,并且您尝试将其分配给列表

你应该做

 List<Candidate> candidates = (List<Candidate>) session.createSQLQuery
               ("select candidate.* from candidate inner join 
                    candidate_skill on candidate.id = candidate_skill.
                        candidate_id inner join skill on 
                  candidate_skill.skill_id = skill.id 
                                          where skill.id = 1").list();

A simple native query example