Struts2 JSON插件不使用;“懒惰”;数据

Struts2 JSON插件不使用;“懒惰”;数据,json,jpa,struts2,lazy-loading,jsonplugin,Json,Jpa,Struts2,Lazy Loading,Jsonplugin,我有一个实体,它有一个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;

我有一个实体,它有一个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,使用中的JSONUtil类(Struts 2的“官方”JSON插件):

我正在使用Hibernate,当我更改fetch=FetchType.EAGER时,上面的代码也会起作用。 我认为延迟加载会生成一个代理对象,这使得它失败


我的问题是:是否可以序列化包含延迟加载属性的对象?

我在使用JSON库时遇到了同样的问题,这确实是因为对象是代理


我发现可以更好地处理Hibernate对象的序列化,但当然它有自己的怪癖,因此您的里程可能会有所不同。

将实体从Hibernate中分离,然后序列化它们

    List<Person> people = personService.findAll();
    String result = JSONUtil.serialize(people);
    System.out.println(result);
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)
    ...