Pregunta

I'm getting a really strange error trying to serialize a JPA/Hibernate entity with FlexJSON. the object itself is nothing fancy; just fields and relationships between other objects.

Caused by: flexjson.JSONException: Error trying to deepSerialize
    at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:61)
    at flexjson.transformer.TransformerWrapper.transform(TransformerWrapper.java:22)
    at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:49)
    at flexjson.transformer.TransformerWrapper.transform(TransformerWrapper.java:22)
    at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:49)
    at flexjson.transformer.TransformerWrapper.transform(TransformerWrapper.java:22)
    at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:49)
    at flexjson.transformer.TransformerWrapper.transform(TransformerWrapper.java:22)
    at flexjson.JSONContext.transform(JSONContext.java:73)
    at flexjson.JSONSerializer.serialize(JSONSerializer.java:377)
    at flexjson.JSONSerializer.serialize(JSONSerializer.java:235)

Caused by: java.lang.reflect.InvocationTargetException
    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 flexjson.BeanProperty.getValue(BeanProperty.java:102)
    at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:37)
    ... 89 more
Caused by: java.lang.IllegalStateException: Cannot call isReadOnlyBeforeAttachedToSession when isReadOnlySettingAvailable == true
at org.hibernate.proxy.AbstractLazyInitializer.isReadOnlyBeforeAttachedToSession(AbstractLazyInitializer.java:308

From reading other (sparse) posts and looking at the code it seems like this is an issue with lazy initialization. This happens repeatedly in a very small amount of cases but those cases are repeatable. My guess is since it seems to be stateful, it's something with the data, but I really have no idea why the data would cause this kind of exception, especially since it is so rare.

¿Fue útil?

Solución

Probably your entity object has a 'lazy loaded' property(ies) which are populated with the values from the database only at the time of their access. And if the hibernate session is already closed at the time of the sterilisation (access of that property) you will get the lazy initialisation exception.

To get rid of this exception you probably have to do one of the following:

  • ensure that hibernate session is not closed at the time of the accessing the lazy loaded properties (serialisation)
  • initialise lazy loaded properties before the session is closed so that it wouldn't need db access even if the session was closed
  • make them not 'lazy loaded'
  • don't serialise lazy loaded properties at all

Otros consejos

The best thing I have experienced to solve it that before you do the deep serialisation you need to put the data like beans, list, map or any data to be serialised in to the HashMap then try to deep serialise that HashMap. You will see the trick is working 100%.

Syed Shahzad

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top