日期:2014-05-17  浏览次数:20680 次

求助,一个Hibernate 问题!!!
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token: class near line 1, column 29 [FROM lixin.study.po.ClassPO class , lixin.study.po.UserPO user where class.classNum=490701]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
at lixin.study.dao.ClassDAO.queryByClassNum(ClassDAO.java:40)
at lixin.study.test.test.main(test.java:15)


这个是我的代码:
public void queryByClassNum(int classNum){
Session session =this.getSession();
String hql="FROM ClassPO class , UserPO user where class.classNum="+classNum;
Query query=session.createQuery(hql);
List<ClassPO> list=query.list();
ClassPO classPO=list.get(0);
Set<UserPO> set=classPO.getUserPOs();
Iterator iterator=set.iterator();
while(iterator.hasNext()){
System.out.println("you");
}
session.close();
}

请问问题出在哪里 ? 是什么原因 , 我是初学hibernate的

------解决方案--------------------
hql语法错误!
FROM ClassPO as c
下面用c 例如c.name 

ClassPO 是数据库的表名 c 相当于是缩写 name就是属性

------解决方案--------------------
FROM 后面跟的是实体类名,最后给个别名
------解决方案--------------------
Java code

String hql="FROM ClassPO as class where class.classNum="+classNum;

------解决方案--------------------
HQL语句有错误,好好看看HQL的语法