hql语句报错
StringBuffer hql=new StringBuffer();
hql.append(" select tm from TModel tm ,TAdminRight tar where tm.id= tar.tmodelId");
hql.append(" tar.tadminId="+tadminId);//and
hql.append(" order by tm.modelNo asc");
//this.currentSession().createQuery(hql.toString()).list();
List<TModel> list= this.find(hql.toString());
具体报错内容:
2015-09-20 20:23:54,555 ERROR org.hibernate.hql.internal.ast.ErrorCounter (ErrorCounter.java:78) :line 1:154: unexpected token: tar
2015-09-20 20:23:54,556 ERROR org.hibernate.hql.internal.ast.ErrorCounter (ErrorCounter.java:73) :line 1:154: unexpected token: tar
line 1:154: unexpected token: tar
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:772)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:323)
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:186)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at org.springframework.orm.hibernate4.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:868)
at org.springframework.orm.hibernate4.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:864)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
at org.springframework.orm.hibernate4.HibernateTemplate.find(HibernateTemplate.java:864)
at com.wangtiansoft.cab.persistence.hibernate.GenericHibernateDAO.find(GenericHibernateDAO.java:179)
at com.wangtiansoft.cab.persistence.dao.SysUserDao.getTAdminRight(SysUserDao.java:126)
at com.wangtiansoft.cab.cms.user.service.impl.SysUserServiceImpl.getTAdminRight(SysUserServiceImpl.java:142)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
经检测发现hql语句中“ hql.append(" tar.tadminId="+tadminId);”语句少了“and” 关键字,修改后
hql.append(" select tm from TModel tm ,TAdminRight tar where tm.id= tar.tmodelId");
hql.append(" and tar.tadminId="+tadminId);//and
hql.append(" order by tm.modelNo asc");
问题解决。