如何修复org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:u

时间:2019-05-31 19:04:36

标签: java postgresql hibernate jpa

我正在尝试获取User对象,该User对象除了用户名和密码外,还具有一个布尔变量,该布尔变量说明是否已删除它。布尔变量称为delete。在我的数据库中,所有用户实体都是假的。

我尝试了ClassicalSyntax,但它又给出了另一个错误。

public User loginUser(String username, String password) {
        TypedQuery<User> query = em.createQuery("Select u from User u " + "where u.username LIKE "
                + "?1 AND u.password LIKE " + "?2 AND u.delete =:delete", User.class);
        query.setParameter(1, username);
        query.setParameter(2, password);
        query.setParameter("delete", false);
        User foundUser = query.getSingleResult();
        return foundUser;

    }

它与其他两个u定义u.username和u.password一起使用,但是当我添加u.delete时,出现此错误。我希望它返回对象。

SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/webproject] threw exception [java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: u near line 1, column 101 [Select u from com.ikubinfo.project.model.User u where u.username LIKE ?1 AND u.password LIKE ?2 AND u.delete =:delete]] with root cause
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: u near line 1, column 101 [Select u from com.ikubinfo.project.model.User u where u.username LIKE ?1 AND u.password LIKE ?2 AND u.delete =:delete]
    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 org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:342)
    at com.ikubinfo.project.repository.UserRepository.loginUser(UserRepository.java:34)
    at com.ikubinfo.project.controller.UserResource.deleteUser(UserResource.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)

2 个答案:

答案 0 :(得分:1)

delete不能用作属性名称,请尝试重命名吗?

答案 1 :(得分:1)

另一种选择是在实体类中对其进行转义: @Column(name = "\"delete\"")