java - Hibernate HQL错误-意外 token :

原文 标签 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)


线程“主”中的异常java.lang.NullPointerException
    在com.SDC.View.Main.main(Main.java:58)

最佳答案

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

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

关于java - Hibernate HQL错误-意外 token :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25926222/

相关文章:

JavaFX - 添加一个 ScrollPane 弹出窗口,当用户点击它时关闭

复杂连接的 Hibernate 映射

java - JPA/Hibernate @MappedSuperclass 和 @Inheritance(strategy = InheritanceType.JOINED) 关系

c# - iTextSharp 7 : Proper Way to Resize Stamps?

java - lambda 数组有缺点吗?

java - 从PNG图像中提取元数据

java - 为什么注释成员不支持静态方法引用类型?

java - Maven MOJO : Get execution configuration in runtime

java - 写入volatile变量后会发生什么?

java - 使用JPA/Hibernate构造函数表达式来防止“n + 1选择”?