-->
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.  [ 5 posts ] 
Author Message
 Post subject: LazyInitializationException: cannot access loading collectio
PostPosted: Fri Apr 29, 2005 12:01 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
Aparently my code is doing something that freaks out Hibernate's lazy-loading proxy. At runtime I get this exception:

Code:
Exception occurred inside setter of com.be.desktopbeautifier.shared.AbstractTheme.parent
org.hibernate.PropertyAccessException: Exception occurred inside setter of com.be.desktopbeautifier.shared.AbstractTheme.parent
        at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:51)
        at org.hibernate.tuple.AbstractTuplizer.setPropertyValues(AbstractTuplizer.java:207)
        at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:171)
        at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2923)
        at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
        at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:494)
        at org.hibernate.loader.Loader.doQuery(Loader.java:413)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208)
        at org.hibernate.loader.Loader.loadCollection(Loader.java:1367)
        at org.hibernate.loader.collection.BatchingCollectionInitializer.initialize(BatchingCollectionInitializer.java:52)
        at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
        at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
        at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1356)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:171)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
        at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:134)
        at com.be.desktopbeautifier.server.DefaultClientTheme.deepCopyOfDescendants(DefaultClientTheme.java:64)
        at com.be.desktopbeautifier.server.DefaultClientTheme.deepCopy(DefaultClientTheme.java:45)
        at com.be.desktopbeautifier.servlet.GetNextImages.getThemeTree(GetNextImages.java:153)
        at com.be.desktopbeautifier.servlet.GetNextImages.processRequest(GetNextImages.java:89)
        at com.be.desktopbeautifier.servlet.GetNextImagesTest.testProcessRequest(GetNextImagesTest.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153)
        at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119)
        at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93)
        at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:224)
        at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)
        at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101)
        at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:224)
        at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
        at java.lang.Thread.run(Thread.java:595)
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 org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:40)
        ... 50 more
Caused by: org.hibernate.LazyInitializationException: cannot access loading collection
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:168)
        at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:61)
        at org.hibernate.collection.PersistentSet.add(PersistentSet.java:158)
        at com.be.desktopbeautifier.shared.AbstractTheme.add(AbstractTheme.java:151)
        at com.be.desktopbeautifier.shared.AbstractTheme.setParent(AbstractTheme.java:123)
        ... 55 more



Basically what is happening is that I have a tree structure. When Node.setParent() is invoked -- connecting it to its parent node -- it invokes parent.add(child) -- connecting it from the other direction as well. When Hibernate sees this "parent.add(child)" call it freaks out and complains with the above exception. I think it's trying to imply that I am accessing a lazy collection before it has been fully loaded.

What is the proposed workaround? Off the top of my head, I can't think up of another way of doing this. First, I need to understand what Hibernate is doing and why it is freaking out, then I will try refactoring my code.

Let me know if you have any idea.

Thank you,
Gili


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 12:21 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
I should point out that both the "parent" and "children" properties map to the same column. To see what I mean, check out my mapping file:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.be.desktopbeautifier.server">
  <class name="Theme" table="theme">
    <id name="id">
      <generator class="native"/>
    </id>
    <discriminator column="class"/>
    <version name="version"/>
   
    <property name="name" type="java.lang.String" not-null="true"/>   
   
    <set name="children" cascade="all" sort="unsorted">
      <key>
        <column name="parent_id"/>
      </key>     
      <one-to-many class="Theme"/>
    </set>

    <many-to-one name="parent" column="parent_id" class="Theme"
    cascade="all"/>   

    <subclass name="AbstractTheme">
       <subclass name="DefaultTheme"/>
    </subclass>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 2:14 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
I've modified my mapping file in order to represent "parent" and "children" as a bidirectional one-to-many mapping. Unfortunately, I am still getting the same error. The new mapping file follows:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : Theme.hbm.xml
    Created on : April 27, 2005, 11:43 AM
-->

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.be.desktopbeautifier.server">
  <class name="Theme" table="theme">
    <id name="id">
      <generator class="native"/>
    </id>
    <discriminator column="class"/>
    <version name="version"/>
   
    <property name="name" type="java.lang.String" not-null="true"/>   
   
    <set name="children" cascade="all" sort="unsorted" inverse="true">
      <key not-null="true">
        <column name="parent_id"/>
      </key>     
      <one-to-many class="Theme"/>
    </set>

    <many-to-one name="parent" column="parent_id" class="Theme"
    cascade="all"/>

    <subclass name="AbstractTheme">
       <subclass name="DefaultTheme"/>
    </subclass>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 9:56 am 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
Removed not-null="true" from children property because I don't think it is allowed for one end of the association to allow null and the other not to when both point to the same column in the same table -- am I wrong?

Now the definition of the column should be identical on both ends. It still does not work though.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 3:11 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
Solved by giving Hibernate a different getter/setter than end-users. The Hibernate method does not try to set the children from setParent() or parent from addChild(), removeChild()... Hibernate seems happy with it.

Gili


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.