本文介绍了org.hibernate.hql.internal.ast.QuerySyntaxException:意外的标记:*在第1行附近的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须登录我的网站

 < form method =postaction =login> 
< input type =textplaceholder =Emailname =email>
< input type =passwordplaceholder =Passwordname =password>
< button type =submit>登入< / button>
< / form>

在控制器中我有

  @RequestMapping(value =/ login,method = RequestMethod.POST)
public String loginUser(@RequestParam(email)String email,
@RequestParam(password )字符串密码,模型模型,HttpSession会话){

User persistedUser = userService.getUserByEmail(email);

if(persistedUser!= null&&& persistedUser.getPassword()。equals(password)){
session.setAttribute(user,persistedUser);
返回家;
} else {
model.addAttribute(error,Incorrect username or password,Please try again!);
返回登录;


$ b $ / code $ / pre

和UserService中我getUserByEmail()作为

  public User getUserByEmail(String email){
return(User)em.createQuery(SELECT * FROM User u WHERE u.email =:email)。setParameter(email,Not Found);
}

当我尝试登录时,我得到

  org.hibernate.hql.internal.ast.QuerySyntaxException:意外的标记:*第1行第8列[SELECT * FROM com.rhcloud.giveme.entity。 User u WHERE u.email =:email] 
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
at org.hibernate.hql.internal.ast。 ErrorCounter.throwQueryException(ErrorCounter.java:109)
位于org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)
位于org.hibernate.hql.internal.ast。 QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
位于org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
位于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.Qu在org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:AbstractSessionImpl.java:1)中
$ or $ $ $ b $
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java .lang.reflect.Method.invoke(来源不明)
在org.springframework.orm.jpa.ExtendedEntityManagerCreator $ ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
$处Proxy76.createQuery(来源不明)
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)
。在org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
$处Proxy70.createQuery(来源不明)在com.rhcloud.giveme.service.UserService
。 getUserByEmail(UserService.java:33)
at com.rhcloud.giveme.service.UserService $$ FastClassBySpringCGLIB $$ 11c5f2a8.invoke(< generated>)
at org.springframework.cglib.proxy.MethodProxy .invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
at com.rhcloud.giveme.service.UserService $$ EnhancerBySpringCGLIB $$ 79be29e2.getUserByEmail(< generated>)
at com.rhcloud.giveme.controller.LoginController.loginUser(LoginCont roller.java:31)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110 )
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter .handleInternal(RequestMappingHandlerAdapter.java:705)
a (org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
在org.springframework .web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
在javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
在org.springframework.web.servlet.FrameworkServlet 。服务(FrameworkServlet.java:842)
在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java :303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomca t.websocket.server.WsFilter.doFilter(WsFilter.java:52)
处org.apache.catalina org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
。 core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve。调用在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
(StandardContextValve.java:122)
在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve。
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java :423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:318)$ b $ java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java .util.concurrent.ThreadPoolExecutor $ Worker.run(未知源)
at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)
at java.lang。 Thread.run(Unknown Source)

如何为getUserByEmail()编写正确的查询,可以摆脱这种异常。

解决方案

您必须省略 SELECT *

  return(User)em.createQuery(FROM User u WHERE u.email =:email ).setParameter(email,Not Found); 


I have for to login my site

<form method="post" action="login">
    <input type="text" placeholder="Email" name="email">
    <input type="password" placeholder="Password" name="password">
    <button type="submit">Login</button>
</form>

in controller I have

@RequestMapping(value="/login",method=RequestMethod.POST)
    public String loginUser(@RequestParam("email") String email,
            @RequestParam("password") String password,Model model,HttpSession session){

        User persistedUser = userService.getUserByEmail(email);

        if(persistedUser!=null && persistedUser.getPassword().equals(password)){
            session.setAttribute("user", persistedUser);
            return "home";
        }else{
            model.addAttribute("error", "Incorrect username or password, Please try again!");
            return "login";
        }

    }

and in UserService I getUserByEmail() as

public User getUserByEmail(String email) {
        return (User) em.createQuery("SELECT * FROM User u WHERE  u.email = :email").setParameter(email, "Not Found");
    }

when i try to log in I get

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [SELECT * FROM com.rhcloud.giveme.entity.User u WHERE  u.email = :email]
    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:190)
    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:328)
    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.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
    at $Proxy76.createQuery(Unknown Source)
    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.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
    at $Proxy70.createQuery(Unknown Source)
    at com.rhcloud.giveme.service.UserService.getUserByEmail(UserService.java:33)
    at com.rhcloud.giveme.service.UserService$$FastClassBySpringCGLIB$$11c5f2a8.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
    at com.rhcloud.giveme.service.UserService$$EnhancerBySpringCGLIB$$79be29e2.getUserByEmail(<generated>)
    at com.rhcloud.giveme.controller.LoginController.loginUser(LoginController.java:31)
    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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

How can I write correct query for getUserByEmail(), so that I can get rid of this exception.

解决方案

You have to omit the SELECT * part, it's implicit:

return (User) em.createQuery("FROM User u WHERE  u.email = :email").setParameter(email, "Not Found");

这篇关于org.hibernate.hql.internal.ast.QuerySyntaxException:意外的标记:*在第1行附近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:28