I am developing web application using restful web service
. I have used hibernate
and POJO
to mysql communication
.
I have database which has the user table and the primary is the empid
. 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).
I have used this one to get the single row from DB where empid=277;
// Hardcoded here
new_user = (User) session.get(User.class, (long)277);
I want to retrieve record from other column like employeeid( NOT empid,empid is primary key)
select * user where employeeid="XX-123XD"
I have passed the employeeid
from html page which is string.
I have written web service like
@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;
}
But i get the error like,
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)
If I removed Query statements, and uncomment the code then that block is
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();
then it works and gives the expected result for empid=277
which is primary key.
What I am doing wrong with Query. And Is there any other way with hibernate to get data without primary key like
`new_user = (User) session.get(User.class, employeeid);`
|
String(Not the primary key)
My jsp file which gets the JSON object is
<%
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);
%>
I have tried using using new_user = (User) query.uniqueResult();
Then also same error occurred JSONObject jsonobj = cliresponse.getEntity(JSONObject.class);
Thank you
Please use Query method uniqueResult().
Query query = session.createQuery("from user where employeeid= :employee");
query.setParameter("employee", employeeid);
new_user = (User) query. uniqueResult();
the client side exception you posted doesn't help, can you provide the server side exception stacktrace? we have to know what hibernate complaints
resolved,
instead of
Query query = session.createQuery("from user where employeeid= :employee");
query.setParameter("employee", employeeid);
I have added,
Query query = session.createQuery("from user where employeeid='" + employeeid + "'");
And it works fine
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.