views:

2040

answers:

2

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 ?

+1  A: 

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.

Tore A.
A: 

Same problem with Oracle.

java.lang.IllegalAccessException: Class com.googlecode.jsonplugin.JSONWriter can not access a member of class oracle.jdbc.driver.PhysicalConnection with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
    at java.lang.reflect.Method.invoke(Method.java:588)
    at com.googlecode.jsonplugin.JSONWriter.bean(JSONWriter.java:215)
    at com.googlecode.jsonplugin.JSONWriter.process(JSONWriter.java:146)
    ...
    at com.googlecode.jsonplugin.JSONWriter.value(JSONWriter.java:116)
    at com.googlecode.jsonplugin.JSONWriter.write(JSONWriter.java:84)
    at com.googlecode.jsonplugin.JSONUtil.serialize(JSONUtil.java:70)
    at com.googlecode.jsonplugin.JSONResult.execute(JSONResult.java:114)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
    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.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:265)
    at org.acegisecurity.ui.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
    at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:124)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
    at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
    at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:149)
    at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:98)
    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:128)
    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:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)