在使用Json-Lib将Java对象转成JsonString时,一般情况下,我们都使用JSONObject.fromObject(Java 对象) 就可以了。但是在使用hibernate来操作数据库得到对象进行转换时就会抛出异常。

异常为:

JSONObject.fromObject(hibernate对象)
出现以下Exception


net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
net.sf.json.JSONObject._fromBean(JSONObject.java:959)

detaiMessage:Positioned Update not supported



解决方法如下 :使用以下代码来

JsonConfig jsonConfig=new JsonConfig();
jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
JSONObject.fromObject(hibernate对象,jsonConfig);