0

我正在尝试在 Java Web 中使用 JSON。通过调用 JSONArray.fromObject() 将 List 转换为 JSONArray,但抛出异常如下:

java.sql.SQLException: Positioned Update not supported.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.ResultSetImpl.getCursorName(ResultSetImpl.java:2130)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2170)
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1332)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:918)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
    at net.sf.json.JSONArray._processValue(JSONArray.java:2514)
    at net.sf.json.JSONArray.processValue(JSONArray.java:2539)
    at net.sf.json.JSONArray.addValue(JSONArray.java:2526)
    at net.sf.json.JSONArray._fromCollection(JSONArray.java:1057)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
    at org.vclub.model.json.Page2Json.getJsonFromList(Page2Json.java:11)
    at org.vclub.controller.TopicController.loadMoreTopic(TopicController.java:91)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:185)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:502)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:465)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:863)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:851)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:767)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache
.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

其实按照网上说的,有两种解决方案,修改struts.xml或者删除DAO字段的getter方法。我没有使用struts,所以我选择删除服务中DAO字段的getter方法。但它仍然无法正常工作。我在这里寻求帮助。提前致谢

4

1 回答 1

0

这似乎是一个Hibernate 延迟加载问题。

如果您要格式化的对象是某种休眠模型类,并将这些类配置为延迟加载,则会发生此问题。

原因是hibernate使用cglib动态生成这个模型类(使用子类方式生成类),这些子类包含“hibernateLazyInitializer”等新属性。然后 JSONObject 也会尝试序列化这些新属性。然后发生异常。

您可以尝试添加以下代码来解决此问题:

JsonConfig cfg = new JsonConfig();
cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
JSONObject json = JSONObject.fromObject(obj,cfg);
于 2013-07-03T07:55:03.957 回答