1 Reply Latest reply on Aug 4, 2009 10:17 PM by Jorge Ruiz

    QuerySyntaxException

    swathi samala Newbie
      Hi all,
      Actually I have done an application which registers new user, if the username already exists it wont add it to the database and throws an error message saying username already exists, I want to do the same with email id also, so I have made slight modification in query of the action class but it throwing a QuerySyntaxException.
      Can anybody please help me out.

      Here is the bean class
      ----------------------
      package com.manam.loan.entity;

      import java.io.Serializable;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import javax.persistence.UniqueConstraint;

      import org.hibernate.annotations.Immutable;
      import org.hibernate.validator.Email;
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotEmpty;
      import org.hibernate.validator.Pattern;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.faces.Validator;

      import com.sun.istack.internal.NotNull;



      @Entity(name="newUser")
      @Name("newUser")
      @UniqueConstraint(columnNames={"EmailId"})

      public class NewUser implements Serializable {
           private static final long serialVersionUID = 1881413500711441951L;
           
           private String userName;
           private String password;
           private String confirmPassword;
           private String fullName;
           private String mobileNumber;
           private String emailId;

           @NotNull
           @Length(min = 3, max = 25)
           @Pattern(regex="[a-zA-Z]?[a-zA-Z0-9_]+",
               message="Member name must start with a letter, and only contain letters, numbers or underscores")

           @Id@Column(name="UserName")
           public String getUserName() {
                return userName;
           }
           public void setUserName(String userName) {
                this.userName = userName;
           }
           
           @NotNull
           @Length(min = 3, max = 25)
          @Pattern(regex = "^\\S(\\S)*$", message = "Invalid Password!")
           @Column(name="Password")
           public String getPassword() {
                return password;
           }
           public void setPassword(String password) {
                this.password = password;
           }
           @NotNull
           @Length(min = 3, max = 25)
           @Pattern(regex = "^\\S(\\S)*$", message = "Invalid Password!")
           @Column(name="ConfirmPassword")
           public String getConfirmPassword() {
                return confirmPassword;
           }
           public void setConfirmPassword(String confirmPassword) {
                this.confirmPassword = confirmPassword;
           }
           
           @NotNull
           @Length(min = 3, max = 25)
           @Pattern(regex="[A-Za-z]+[ A-Za-z]+" , message="Invalid Name! Numbers/Special characters are not allowed!")
           @Column(name="FullName")
           public String getFullName() {
                return fullName;
           }
           public void setFullName(String fullName) {
                this.fullName = fullName;
           }
           
           @NotEmpty
           @Length(max=15)
           @Pattern(regex = "(\\d*\\d*)" , message="Use Only Integers. No spaces or special characters are allowed")
           @Column(name="MobileNumber")
           public String getMobileNumber() {
                return mobileNumber;
           }
           public void setMobileNumber(String mobileNumber) {
                this.mobileNumber = mobileNumber;
           }
           
           @NotNull
           @Length(min = 3, max = 35)
           @Email
           @Column(name="EmailId", unique = true)
           public String getEmailId() {
                return emailId;
           }
           public void setEmailId(String emailId) {
                this.emailId = emailId;
           }
           
           
      }
      Here is action class
      ----------------------
      package com.manam.loan.session;

      import java.io.Serializable;
      import java.util.List;


      import javax.persistence.EntityManager;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.faces.FacesMessages;
      import org.jboss.seam.security.Identity;

      import com.manam.loan.entity.NewUser;



      @Name("createUser")
      public class SaveAction implements Serializable {
           @In
           private NewUser newUser;
           
           @In (create=true)
           private EntityManager entityManager;
           
           @In
           private FacesMessages facesMessages;
           
           
           private boolean saved;
           
           public String save()
           {     
                if(!(newUser.getPassword().equals(newUser.getConfirmPassword()))){
                     FacesMessages.instance().add("Passwords do not match");
                     System.out.println("password not matched");
                     return "passwordnotmatch";
                }
                  List existing = entityManager.createQuery("select u.userName,u.emailId from newUser u where u.userName=#{newUser.userName}," +
                            "u.emailId=#{newUser.userName}")
                       .getResultList();
                    if (existing.size()==0)
                    {
                         entityManager.persist(newUser);
                       facesMessages.add("Successfully saved as #{newUser.userName}");
                       facesMessages.add("Successfully saved as #{newUser.emailId}");
                       saved = true;
                       return "success";
                    }
                    else
                    {
                       facesMessages.add("UserName #{newUser.userName} already exists");
                       facesMessages.add("UserName #{newUser.emailId} already exists");
                      
                    }
                return "fail";
          }
           
           
           
           public boolean isSaved() {
                return saved;
           }

           public void setSaved(boolean saved) {
                this.saved = saved;
           }
           
      }

      Exeption
      --------------
      11:58:18,875 ERROR [PARSER] line 1:87: unexpected token: ,
      11:58:18,890 FATAL [application] java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: , near line 1, column 87 [select u.userName,u.emailId from com.manam.loan.entity.NewUser u where u.userName=:el1,u.emailId=:el2]
      javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: , near line 1, column 87 [select u.userName,u.emailId from com.manam.loan.entity.NewUser u where u.userName=:el1,u.emailId=:el2]
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
           at javax.faces.component.UICommand.broadcast(UICommand.java:383)
           at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
           at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
           at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
           at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
           at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
           at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
           at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
           at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
           at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
           at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
           at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
           at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
           at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
           at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
           at java.lang.Thread.run(Unknown Source)
      Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: , near line 1, column 87 [select u.userName,u.emailId from com.manam.loan.entity.NewUser u where u.userName=:el1,u.emailId=:el2]
           at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:616)
           at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:95)
           at org.jboss.seam.persistence.EntityManagerProxy.createQuery(EntityManagerProxy.java:67)
           at com.manam.loan.session.SaveAction.save(SaveAction.java:38)
           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.jboss.seam.util.Reflections.invoke(Reflections.java:21)
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
           at com.manam.loan.session.SaveAction_$$_javassist_2.save(SaveAction_$$_javassist_2.java)
           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.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329)
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342)
           at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
           at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
           at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
           at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
           ... 49 more
      Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: , near line 1, column 87 [select u.userName,u.emailId from com.manam.loan.entity.NewUser u where u.userName=:el1,u.emailId=:el2]
           at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
           at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
           at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
           at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
           at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
           at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
           at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
           at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
           at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
           at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
           at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
           at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
           at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:92)
           ... 79 more