• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialize a Persistent Hibernate Object gives lazy initialization errors

 
Vaneet Grewal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to serialize (to XML) a Hibernate Persistent Object that I have retrieved in a Hibernate find query.

Upon conversion using the JDK XML encoder I get Hibernate lazy intialisation errors.

Has anyone seen these before? Any advice would be great.

The errors look like so -->

com.gs.tbs.ppl.payment.PaymentDetailsImpl.achPaymentMethod - no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.gs.tbs.ppl.payment.PaymentDetailsImpl.achPaymentMethod - no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:191)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:183)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.size(PersistentSet.java:110)
at java.util.AbstractSet.equals(AbstractSet.java:73)
at java.beans.DefaultPersistenceDelegate.equals(DefaultPersistenceDelegate.java:203)
at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:216)
at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:250)
at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:394)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:54)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:254)
at java.beans.Encoder.writeExpression(Encoder.java:259)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:369)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:97)
at java.beans.Encoder.writeObject(Encoder.java:54)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:254)
at java.beans.Encoder.writeExpression(Encoder.java:259)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:369)
at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:215)
at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:250)
at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:394)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:54)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:254)
at java.beans.Encoder.writeExpression(Encoder.java:259)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:369)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:97)
at java.beans.Encoder.writeObject(Encoder.java:54)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:254)
at java.beans.Encoder.writeExpression(Encoder.java:259)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:369)
at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:215)
at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:250)
at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:394)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:54)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:254)
at java.beans.Encoder.writeExpression(Encoder.java:259)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:369)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:97)
at java.beans.Encoder.writeObject(Encoder.java:54)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:254)
at java.beans.Encoder.writeObject1(Encoder.java:186)
at java.beans.Encoder.cloneStatement(Encoder.java:199)
at java.beans.Encoder.writeStatement(Encoder.java:230)
at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:328)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:257)
at com.gs.tbs.core.framework.controller.message.converter.ObjectXMLConverter.convertToXMLString(ObjectXMLConverter.java:143)
at com.gs.tbs.core.framework.controller.message.converter.ObjectXMLConverter.toMessage(ObjectXMLConverter.java:92)
at com.gs.tbs.core.framework.controller.message.writer.JmsMessageWriter.write(JmsMessageWriter.java:69)
at com.gs.tbs.ppl.servers.cancel.SuccessfulCancellationAction.execute(SuccessfulCancellationAction.java:112)
at com.gs.tbs.core.framework.controller.CoreController.onEvent(CoreController.java:162)
at com.gs.tbs.core.framework.controller.action.AbstractAction.triggerEvent(AbstractAction.java:74)
at com.gs.tbs.ppl.servers.cancel.MessageGenerationAction.execute(MessageGenerationAction.java:93)
at com.gs.tbs.core.framework.controller.action.LinearActionSequence.execute(LinearActionSequence.java:86)
at com.gs.tbs.core.framework.controller.CoreController.onEvent(CoreController.java:162)
at com.gs.tbs.core.framework.controller.message.source.TransactionalEventTriggeringMessageSource$1.doInTransaction(TransactionalEventTriggeringMessageSource.java:105)
at com.gs.tbs.core.transaction.TransactionTemplate.execute(TransactionTemplate.java:72)
at com.gs.tbs.core.framework.controller.message.source.TransactionalEventTriggeringMessageSource.doCallbacks(TransactionalEventTriggeringMessageSource.java:83)
at com.gs.tbs.core.framework.controller.message.source.TransactionalEventTriggeringMessageSource.open(TransactionalEventTriggeringMessageSource.java:52)
at com.gs.tbs.core.framework.controller.CoreController.start(CoreController.java:190)
at com.gs.tbs.core.framework.server.ServerImpl.start(ServerImpl.java:65)
at com.gs.tbs.core.framework.server.ServerImpl.boot(ServerImpl.java:105)
at com.gs.tbs.core.framework.server.ServerBootstrap.main(ServerBootstrap.java:79)
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.gs.tbs.ppl.payment.PaymentDetailsImpl.achPaymentMethod - no session or session was closed
Continuing ...
2006-03-22 17:52:58,362 ERROR [hibernate.LazyInitializationException ] () failed to lazily initialize a collection of role:
I have already tried changing the hibernate fetch options to lazy = true or false, but to no avail.
Also I have tried using,
public static void initialize(java.lang.Object object) throws org.hibernate.HibernateException
before invoking the XML conversion and again I get the same problem. The persistent set objects that the logs complain about are initialised and I have even confirmed that using isInitialized just before the conversion.
The generics on the get for the persistent set does not make a difference either whether I implement generics or not.
Also cascade-all does not solve the problem (and also would not be an ideal solution anyway as it is overkill).

The code where the XML conversion occurs is as follows:


Any comments or advice?

[Edited to make code more readable - Paul Sturrock]
[ March 23, 2006: Message edited by: Paul Sturrock ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

no session or session was closed

This means that your code (either directly or via XMLEncoder) is trying to access a property on your POJO object which has not yet had its value populated from the database. Hibernate will try to populate these values then and there - if there is no open session available to it then its got no access to the database and can't populate the property, hence the exception message.

To get the output you want with a closed session, you will need eagerly fetch all the data that the object can contain. Alternatively, use an open session.
[ March 23, 2006: Message edited by: Paul Sturrock ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic