java.lang.ClassCastException: org.hibernate.hql.internal.ast.tree.SqlNode cannot be cast to org.hibernate.hql.internal.ast.tree.PathNode
1、该异常通常是在Hibernate中使用HQL查询语言时出错:
一般从数据库中获得的字段:
String hql = "select c.classId ,c.className from Class c where c.college = :college"; // 没有错误
如果对获得字段进行封装:
String hql = "select new Class(c.classId,c.className) "+" from Class c "+"where c.college = :college"; // 会报错
原因:Hibernate对HQL查询语言进行转换后与sql不对应或找不到具体的类与之对应
解决办法:在封装的字段的类前加上类所在的包名:
String hql = "select new wanglizheng.hibernate.JavaBean.HQL.Class(c.classId,c.className) "+" from Class c "+"where c.college = :college";
// 这样写,即可解决