title: retrieving data from mysql using hibernate without primary key in restful web service
body: <p>I am developing web application using <code>restful web service</code>.
  I have used <code>hibernate</code> and <code>POJO</code> to <code>mysql communication</code>.</p>
  
  <p>I have database which has the user table and the primary is the <code>empid</code>.
  For getting the single row, I have passed the empid from html page to the web service and return the json object to the html page(javascript is used for parsing).</p>
  
  <p>I have used this one to get the single row from DB <code>where empid=277;</code>  // Hardcoded here</p>
  
  <blockquote>
    <p>new_user = (User) session.get(User.class, (long)277);</p>
  </blockquote>
  
  <p>I want to retrieve record from other column like employeeid( NOT empid,empid is primary key)</p>
  
  <pre><code>select * user where employeeid="XX-123XD"
  </code></pre>
  
  <p>I have passed the <code>employeeid</code> from html page which is string.</p>
  
  <p>I have written web service like </p>
  
  <pre><code>    @POST
      @Path("/getjson")
      @Produces("application/json")
      public JSONObject sendjson(String employeeid) {
  
              Gson gsonobj = new Gson();
              JSONObject jsonobj = null;
  
              SessionFactory fact;
              fact = new Configuration().configure().buildSessionFactory();
              User new_user = null;
  
              Session session = fact.getCurrentSession();
              Transaction tx =  session.beginTransaction();
              Query query = session.createQuery("from user where employeeid= :employee");
              query.setParameter("employee", employeeid);
              //new_user = (User) session.get(User.class, (long)277);
              new_user = (User) query.list();
  
          try {    
              String jsonstr = gsonobj.toJson(new_user);
              jsonobj = new JSONObject(jsonstr);
          } catch (JSONException ex) {
              Logger.getLogger(Authneticate.class.getName()).log(Level.SEVERE, null, ex);
          }
          return jsonobj;
      }
  </code></pre>
  
  <p>But i get the error like, </p>
  
  <pre><code>type Exception report
  
  message com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.codehaus.jettison.json.JSONObject, and Java type class org.codehaus.jettison.json.JSONObject, and MIME media type text/html; charset=utf-8 was not found
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  
  org.apache.jasper.JasperException: com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.codehaus.jettison.json.JSONObject, and Java type class org.codehaus.jettison.json.JSONObject, and MIME media type text/html; charset=utf-8 was not found
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  root cause
  
  com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.codehaus.jettison.json.JSONObject, and Java type class org.codehaus.jettison.json.JSONObject, and MIME media type text/html; charset=utf-8 was not found
      com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:561)
      com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
      org.apache.jsp.HomeUser_jsp._jspService(HomeUser_jsp.java:91)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>If I removed Query statements, and uncomment the code then that block is</p>
  
  <pre><code>    Session session = fact.getCurrentSession();
      Transaction tx =  session.beginTransaction();
      //Query query = session.createQuery("from user where employeeid= :employee");
      //query.setParameter("employee", employeeid);
      new_user = (User) session.get(User.class, (long)277);
      //new_user = (User) query.list();
  </code></pre>
  
  <p>then it works and gives the expected result for <code>empid=277</code> which is primary key.</p>
  
  <p>What I am doing wrong with Query.
  And Is there any other way with hibernate to get data without primary key
  like </p>
  
  <pre><code>`new_user = (User) session.get(User.class, employeeid);`
                                                    |
                                            String(Not the primary key)
  </code></pre>
  
  <p>My jsp file which gets the JSON object is</p>
  
  <pre><code>&lt;%
  
  Client client = Client.create();
  WebResource service = client.resource("http://localhost:8080/ITHelpdesk/webresources/hello/getjson");
  String input = request.getParameter("username");
  ClientResponse cliresponse = service.type("text/html").post(ClientResponse.class,input);
  JSONObject jsonobj = cliresponse.getEntity(JSONObject.class);
  
  %&gt;
  </code></pre>
  
  <p>I have tried using using <code>new_user = (User) query.uniqueResult();</code>
  Then also same error occurred  <code>JSONObject jsonobj = cliresponse.getEntity(JSONObject.class);</code></p>
  
  <p>Thank you</p>