-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: XMLEncoder throwing LazyInitializationException
PostPosted: Wed Nov 03, 2004 1:51 pm 
Newbie

Joined: Wed Nov 03, 2004 1:20 pm
Posts: 2
I am getting an exception when trying to "serialize" an object graph to xml using the jdk 1.4 XMLEncoder class. It seems when a collection is encountered within the graph, then something goes awry.

This only happens after the object has been made persistent. That is, if I encode the object graph before the call to session.save() then, everything works just fine. However, any call to encoder.writeObject() after calling session.save() does not work properly.

I have seen references to a similar problem in the 2.1.5 release notes and a corresponding fix. However, if anyone has experience or would just like to share woes, I'd greatly appreciate it.

I wrote a sample app to illustrate the problem and have included the pertinent snippets below the signagure line.

Thanks for your attention,

Dan


Hibernate version: 2.1.6

Mapping documents:

Code:

<!-- from FieldDefinition.hbm.xml -->
<subclass
            name="com.bequeaith.meta.ChoiceFieldDefinition"
            dynamic-update="false"
            dynamic-insert="false"
            discriminator-value="ChoiceFieldDefinition">

            <set name="choices" lazy="false" inverse="false" cascade="all" sort="unsorted">
               <cache usage="transactional"/>

               <key column="FK_CHOICE_FIELD_DEFINITION">
               </key>

               <one-to-many class="com.bequeaith.meta.Choice"/>
            </set>
         </subclass>

<!--from choice.hbm.xml -->
<class
        name="com.bequeaith.meta.Choice"
        table="CHOICE"
        dynamic-update="false"
        dynamic-insert="false">

        <id name="identifier" column="PK" type="java.lang.Long">
            <generator class="native">
            </generator>
        </id>
       
        <property name="value" type="java.lang.String" update="true" insert="true" access="property" column="VALUE"/>
    </class>



Code between sessionFactory.openSession() and session.close():
Code:
public void testFieldDefinition() throws Exception
   {
      Session session = null;
      XMLEncoder encoder = null;
      try
      {
         SessionFactory factory = new Configuration().configure().buildSessionFactory();

         session = factory.openSession();

         ChoiceFieldDefinition cars = new ChoiceFieldDefinition(CARS);
         cars.addChoice("BMW");
         cars.addChoice("Ferrari");
         cars.addChoice("TVR");
         cars.addChoice("Lamborghini");

         session.saveOrUpdate(cars);
         session.flush();

         assertTrue("Session closed", session.isOpen());
         encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("./testxmloutput.xml")));
         encoder.writeObject(cars);
         encoder.close();
      }
      finally
      {
         session.close();
      }



Full stack trace of any exception that occurs:

Code:
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
   at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:242)
   at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:74)
   at net.sf.hibernate.collection.Set.size(Set.java:106)
   at java.util.AbstractSet.equals(AbstractSet.java:72)
   at java.beans.DefaultPersistenceDelegate.equals(DefaultPersistenceDelegate.java:207)
   at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:220)
   at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:254)
   at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:395)
   at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:103)
   at java.beans.Encoder.writeObject(Encoder.java:55)
   at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
   at java.beans.Encoder.writeExpression(Encoder.java:260)
   at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
   at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
   at java.beans.Encoder.writeObject(Encoder.java:55)
   at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
   at java.beans.Encoder.writeObject1(Encoder.java:192)
   at java.beans.Encoder.cloneStatement(Encoder.java:205)
   at java.beans.Encoder.writeStatement(Encoder.java:236)
   at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:326)
   at java.beans.XMLEncoder.writeObject(XMLEncoder.java:253)
   at com.bequeaith.meta.TestMeta.testFieldDefinition(TestMeta.java:82)
   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:324)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at junit.textui.TestRunner.doRun(TestRunner.java:116)
   at com.intellij.rt.execution.junit2.IdeaJUnitAgent.doRun(IdeaJUnitAgent.java:57)
   at junit.textui.TestRunner.start(TestRunner.java:172)
   at com.intellij.rt.execution.junit.TextTestRunner2.startRunnerWithArgs(TextTestRunner2.java:23)
   at com.intellij.rt.execution.junit2.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:97)
   at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 4:07 pm 
Newbie

Joined: Wed Nov 03, 2004 1:20 pm
Posts: 2
I've done some further research and notice that the collection declared by my code is a java.util.HashSet and after saving the persistent object, it is replaced by net.sf.hibernate.collection.Set. This set seems to want a reference to the session but does not have said reference.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.