title: HQL: Handling special characters
body: <p>I am trying to fetch value from database using <code>HQL</code> but am getting exceptions because value contains special characters. I am not able to figure out why.</p>
  
  <p>Below is the code i am trying:</p>
  
  <pre><code>    HotelMapping hotelMapping = null;
      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      Session session = sessionFactory.openSession();
  
      Transaction tx = session.getTransaction();
      tx.begin();
      String hotelName = "A Fisher's Inn Motel";
      Query query = session.createQuery("from HotelMapping hm where hm.hotelID.hotelName='"+hotelName+"'");
      HotelMapping mapping = query.uniqueResult();
      }
      tx.rollback();
  
      sessionFactory.close();
  </code></pre>
  
  <p>The pojos look like below:
  <strong>Hotel.java</strong></p>
  
  <pre><code>public class Hotel{
      String hotelName;
      double price;
      //getters and setters
  }
  </code></pre>
  
  <p><strong>HotelMapping.java</strong></p>
  
  <pre><code>public class HotelMapping{
  
       @OneToOne(cascade = CascadeType.ALL)
       Hotel hoteID
  
       String location;
  }
  </code></pre>
  
  <p>The query string </p>
  
  <p><code>Query query = session.createQuery("from HotelMapping hm where hm.hotelID.hotelName='"+hotelName+"'");</code>  gives me below exception :</p>
  
  <pre><code>Exception in thread "main" org.hibernate.QueryException: expecting ''', found '&lt;EOF&gt;' [from com.pb.model.HotelMapping hm where hm.hotelID.hotelName='A Fisher's Inn Motel']
  </code></pre>
  
  <p>I tried escaping the apostrophe but with no luck. I ven tried setting the query parameter but again i got exception</p>
  
  <pre><code>query.setParameter("hotelName", "A Fisher's Inn Motel");
  </code></pre>
  
  <p>It says <code>Exception in thread "main" org.hibernate.QueryParameterException: could not locate named parameter [hotelName]</code></p>
  
  <p>Please if someone could help me achieving a generalized solution for the special character handling?</p>