Hibernate HQL出错 - 意外令牌:

时间:2014-09-19 04:08:51

标签: java hibernate annotations hql

***import org.hibernate.Query;***

String hql = "FROM :className WHERE userCreate like ':userName'";
Query query = session.createQuery(hql);
query.setParameter("className", className);
query.setParameter("userName", userName);
List<Node> result = query.list();

有错误

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: : near line 1, column 6 [FROM :className WHERE userCreate like ':userName']
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)
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:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
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 com.SDC.DAO.DAOFileAndFolderService.findUserCreteFileOrFolder(DAOFileAndFolderService.java:36)
at com.SDC.View.Main.main(Main.java:57)

线程中的异常&#34; main&#34;显示java.lang.NullPointerException     在com.SDC.View.Main.main(Main.java:58)

1 个答案:

答案 0 :(得分:3)

您不能将命名参数用于Hibernate查询。您可以通过手动将表名添加到hql字符串中来解决此问题。您的代码看起来会像这样:

String hql = "FROM " + className + " WHERE userCreate like ':userName'";
Query query = session.createQuery(hql);
query.setParameter("userName", userName);
List<Node> result = query.list();