failed to lazily initialize a collection, no session or session was closed

285 views
Skip to first unread message

void.no....@gmail.com

unread,
Jan 25, 2013, 11:25:32 PM1/25/13
to json-io
I got the following exception while trying to call
JsonReader.jsonToJava() on a JSON string (created using
JsonWriter.objectToJson()). I don't understand how Hibernate gets
involved, since the JsonReader is just reading from a JSON string.
But I did check all of our Hibernate mapping files, and every one of
them specifies that lazy loading is disabled. Not only that, but when
I use the Google Gson library, it has no problems writing/reading JSON
with our objects.

Caused by: org.hibernate.LazyInitializationException: failed to lazily
initialize a collection, no session or session was closed
at
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:
383)
at
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:
375)
at
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:
368)
at
org.hibernate.collection.PersistentSet.add(PersistentSet.java:212)
at
com.cedarsoftware.util.io.JsonReader.traverseCollection(JsonReader.java:
1066)
at
com.cedarsoftware.util.io.JsonReader.convertMapsToObjects(JsonReader.java:
777)
at
com.cedarsoftware.util.io.JsonReader.traverseCollection(JsonReader.java:
1064)
at
com.cedarsoftware.util.io.JsonReader.convertMapsToObjects(JsonReader.java:
777)
at
com.cedarsoftware.util.io.JsonReader.convertParsedMapsToJava(JsonReader.java:
724)
at
com.cedarsoftware.util.io.JsonReader.readObject(JsonReader.java:688)
at
com.cedarsoftware.util.io.JsonReader.jsonToJava(JsonReader.java:600)

John

unread,
Nov 13, 2013, 8:09:52 AM11/13/13
to jso...@googlegroups.com
Hibernate wraps Java objects with Proxies so that these proxies get a chance to intercept your getter/setter calls and, if the object is only a shell object with an id, hibernate proxy will go to the database, load the object, and then the calling code will get the data it was asking for.  In order for this 'magic' to happen, the Hibernate object must be attached to a Hibernate session (a Java JDBC connection must be around for access to the database).  

If you are trying to serialize objects that are of a 'Hibernate nature' (HOJO's), you need to make sure that the objects you are serializing out are completely loaded.  That means you would need to walk through the object graph and ensure that it is loaded.  This is where the Hibernate model breaks down in general (I never recommend using Hibernate).  Your object graph needs to be completely self-contained (if it has just one pointer to another object that is not loaded, then you won't be able to serialize the object graph).  

Most apps sent Data Transfer Objects (DTO's) to receiving clients and these DTO objects are either java.util.Map instances or actual Java POJO's with only properties (optional setter / getters on them), and no behavior.  The data is copied from the HOJO's to the DTOs, while the Hibernate objects are in session.  The DTO's are then what is serialized out as the return value (say in an server response to an Ajax call).

John
Reply all
Reply to author
Forward
0 new messages