使用hibernate从mysql中检索数据,而无需在restful Web服务中使用主键

时间:2013-08-02 05:47:48

标签: java json hibernate rest

我正在使用restful web service开发Web应用程序。 我已将hibernatePOJO用于mysql communication

我的数据库有用户表,主要是empid。 为了获得单行,我已经将hid页面中的empid传递给了web服务,并将json对象返回到html页面(javascript用于解析)。

我用这个来获取数据库where empid=277; //这里硬编码

的单行
  

new_user =(User)session.get(User.class,(long)277);

我想从其他列中检索记录,例如employeeid(NOT empid,empid是主键)

select * user where employeeid="XX-123XD"

我已经从html页面传递了employeeid,这是一个字符串。

我写过像

这样的网络服务
    @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;
    }

但是我得到了错误,

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)

如果我删除了Query语句,并取消注释代码,则该块为

    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();

然后它起作用并给出作为主键的empid=277的预期结果。

我在查询时遇到了什么问题。 还有其他方法可以让hibernate获取没有主键的数据 喜欢

`new_user = (User) session.get(User.class, employeeid);`
                                                  |
                                          String(Not the primary key)

我获取JSON对象的jsp文件是

<%

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);

%>

我尝试过使用new_user = (User) query.uniqueResult(); 然后也发生了同样的错误JSONObject jsonobj = cliresponse.getEntity(JSONObject.class);

谢谢

3 个答案:

答案 0 :(得分:0)

请使用Query方法uniqueResult()。

 Query query = session.createQuery("from user where employeeid= :employee");
 query.setParameter("employee", employeeid);
 new_user = (User) query. uniqueResult();

答案 1 :(得分:0)

您发布的客户端异常没有帮助,您能提供服务器端异常堆栈跟踪吗?我们必须知道什么是hibernate投诉

答案 2 :(得分:0)

解决,

而不是

Query query = session.createQuery("from user where employeeid= :employee");
query.setParameter("employee", employeeid);

我已添加,

Query query = session.createQuery("from user where employeeid='" + employeeid + "'");

它工作正常