2 Replies Latest reply on Oct 4, 2010 10:20 AM by lvdberg

    How to Pass symbolic character in query created by entity manager

    sadiquekatihar
      i have a query created in entity manager. when i try to execute this query using hibernet . it throws a genuine error message that i am using a special character @ in this query . But i don't know how can i pass a symbolic character in hibernet query.
      My query is

      private boolean isUniqueEmail(String emailadd)
          {
              String myquery = "select u from AuthenticationInformation u where u.email=" + emailadd;
              Query q = entityManager.createQuery(myquery);
             // q.setParameter(":email", emailadd);
              List res = q.getResultList();
              if (res.size()>0)
              {
                  return false;
              }
              else
              {
                  return true;
              }
          }

      value passing as argument to this method is abc@gmail.com

      ERROR : message is

      Caused by org.hibernate.QueryException with message: "unexpected char: '@' [select u from entity.timi.dot.ca.gov.AuthenticationInformation u where u.email=Sadique4t.ktr@gmail.com]

      Please help me regarding this issue.

      Plz help me regarding i
        • 1. Re: How to Pass symbolic character in query created by entity manager
          cash1981

          Can you also show how you send this in your form/view?


          My guess is that you are either sending as request parameter or get method as parameter in method.
          This is not the preferred way of doing it.

          • 2. Re: How to Pass symbolic character in query created by entity manager
            lvdberg

            Hi,


            You're using String concatenation to create the query string. It can work, but it is realy eror-prone such as in your case. You're missing the single quotes around the email address that's the reason that it doesn't work.


            Try the following:



             String myquery = "select u from AuthenticationInformation u where u.email=' " + emailadd + " ' ";



            The best way to prevent these kind of error is to use parameters in you query as you already tried. Something like:




             String myquery = "select u from AuthenticationInformation u where u.email = :email";
            q.setParameter("email", emailadd);



            The name of the parameter is WITHOUT the :.


            Leo