Struts2 JSON 插件无法与“lazy”一起使用 数据
我有一个具有 OneToOne 关系的实体,该实体是延迟获取的:
@Entity
public class Person {
@Id
private Integer id;
@Column(length=60)
private String address;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="idProvince")
private Province province;
}
这是我所做的测试,尝试获取所有实体并将它们序列化为 JSON,使用 JSONPlugin (Struts 2 的“官方”json 插件):
List<Person> people = personService.findAll();
String result = JSONUtil.serialize(people);
System.out.println(result);
这是我得到的异常(当我使用这个时,同样的异常带有 Struts2 Action 和 @JSON 注释的插件):
com.googlecode.jsonplugin.JSONException: java.lang.IllegalAccessException:
Class com.googlecode.jsonplugin.JSONWriter can not access a member of class
org.postgresql.jdbc4.AbstractJdbc4Statement with modifiers "public"
at com.googlecode.jsonplugin.JSONWriter.bean(JSONWriter.java:237)
at com.googlecode.jsonplugin.JSONWriter.process(JSONWriter.java:159)
at com.googlecode.jsonplugin.JSONWriter.value(JSONWriter.java:125)
at com.googlecode.jsonplugin.JSONWriter.array(JSONWriter.java:407)
at com.googlecode.jsonplugin.JSONWriter.process(JSONWriter.java:149)
at com.googlecode.jsonplugin.JSONWriter.value(JSONWriter.java:125)
at com.googlecode.jsonplugin.JSONWriter.write(JSONWriter.java:93)
at com.googlecode.jsonplugin.JSONWriter.write(JSONWriter.java:76)
at com.googlecode.jsonplugin.JSONUtil.serialize(JSONUtil.java:62)
...
我正在使用 Hibernate,并且当我更改 fetch=FetchType.EAGER 时,上面的相同代码可以工作。 我认为延迟加载会生成一个代理对象,这会导致它失败。
我的问题是:是否可以序列化包含延迟加载属性的对象?
I have an Entity with a OneToOne relation that is fetched lazily:
@Entity
public class Person {
@Id
private Integer id;
@Column(length=60)
private String address;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="idProvince")
private Province province;
}
This is the test I do, trying to get all the entities and to serialize them as JSON, using the JSONUtil class in JSONPlugin (the 'official' json plugin for Struts 2):
List<Person> people = personService.findAll();
String result = JSONUtil.serialize(people);
System.out.println(result);
And this is the exception I get (the same exception when I use this plugin with a Struts2 Action and the @JSON annotation):
com.googlecode.jsonplugin.JSONException: java.lang.IllegalAccessException:
Class com.googlecode.jsonplugin.JSONWriter can not access a member of class
org.postgresql.jdbc4.AbstractJdbc4Statement with modifiers "public"
at com.googlecode.jsonplugin.JSONWriter.bean(JSONWriter.java:237)
at com.googlecode.jsonplugin.JSONWriter.process(JSONWriter.java:159)
at com.googlecode.jsonplugin.JSONWriter.value(JSONWriter.java:125)
at com.googlecode.jsonplugin.JSONWriter.array(JSONWriter.java:407)
at com.googlecode.jsonplugin.JSONWriter.process(JSONWriter.java:149)
at com.googlecode.jsonplugin.JSONWriter.value(JSONWriter.java:125)
at com.googlecode.jsonplugin.JSONWriter.write(JSONWriter.java:93)
at com.googlecode.jsonplugin.JSONWriter.write(JSONWriter.java:76)
at com.googlecode.jsonplugin.JSONUtil.serialize(JSONUtil.java:62)
...
I am using Hibernate and the same code above works when I change fetch=FetchType.EAGER.
I think lazy loading generates a proxy-object, and that makes it fail.
My question is : Is it possible to serialize objects that contain lazily loaded attributes ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用 JSON-lib 时遇到了同样的问题,这确实是因为该对象是代理。
我发现 Google-Gson 可以更好地处理 Hibernate 对象的序列化,但当然它有它有自己的怪癖,所以你的里程可能会有所不同。
I had the same problem while using JSON-lib, and it is indeed because the object is a proxy.
I found Google-Gson handles serialization of Hibernate objects better, but of course it has its own quirks, so your mileage may vary.
从 Hibernate 和 Hibernate 中分离实体 然后将它们序列化。
Detach the Entities from the Hibernate & then serialize them.