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.
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: