-->
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: cascaded delete to lazily initialized collection of <any&
PostPosted: Wed Nov 26, 2003 5:46 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 9:28 pm
Posts: 25
I have a parent object that has a lazily initialized collection of objects mapped with the <any> mapping. The cascade rule for this collection is set to "all-delete-orphan". When the parent object is deleted, an ObjectDeletedException occurs. The parent object also has a collection of many-to-one objects that is configured the same way. These objects are deleted successfully when the parent object is deleted.

In the likelyhood that I'm doing something stupid, I thought I'd post this on the forum. I do have a whittled down test case that I could submit to JIRA if desired.

Here are the mappings:

Foo (the parent object) ...
Code:
 
<hibernate-mapping>
   <class name="test.Foo" table="FOO">
      <id name="oid" column="OID" type="long" unsaved-value="null">
         <generator class="increment"/>
      </id>
      <property name="text" column="TEXT" type="string"/>
      <bag name="oneToManyItems" inverse="true" cascade="all-delete-orphan" lazy="true" access="field">
         <key column="FOO_OID"/>
         <one-to-many class="test.OneToManyItem"/>
      </bag>
      <bag name="anyItems" inverse="true" cascade="all-delete-orphan" lazy="true" access="field"
           where="ANCHOR_CLASS = 'test.Foo'">
         <key column="ANCHOR_OID"/>
         <one-to-many class="test.AnyItem"/>
      </bag>
   </class>
</hibernate-mapping>


OneToManyItem ...
Code:
<hibernate-mapping>
   <class name="test.OneToManyItem" table="ONE_TO_MANY_ITEM">
      <id name="oid" column="OID" type="long" unsaved-value="null">
        <generator class="increment"/>
      </id>
      <many-to-one name="foo" column="FOO_OID" class="test.Foo"/>
      <property name="text" column="TEXT" type="string"/>
   </class>
</hibernate-mapping>


AnyItem ...
Code:
<hibernate-mapping>
   <class name="test.AnyItem" table="ANY_ITEM">
      <id name="oid" column="OID" type="long" unsaved-value="null">
        <generator class="increment"/>
      </id>
      <any name="anchor" id-type="long" meta-type="class">
         <column name="ANCHOR_CLASS"/>
         <column name="ANCHOR_OID"/>
      </any>
      <property name="text" column="TEXT" type="string"/>
   </class>
</hibernate-mapping>



The test case code that generates the exception
Code:
       
System.out.println("testFooDeleteWithAnyItems()");

        Session session = sf.openSession();

        // create a Foo object and add an AnyItem to it
        Foo foo = new Foo();
        foo.setText("Testing Delete of Foo that has AnyItems");

        AnyItem anyItem = new AnyItem();
        anyItem.setAnchor(foo);
        anyItem.setText("any item text");
        foo.getAnyItems().add(anyItem);


        session.save(foo);
        session.flush();

        Long fooOid = foo.getOid();

        session.connection().commit();
        session.close();

        // Load the Foo object and delete it
        session = sf.openSession();
        System.out.println("Loading Foo");
        foo = (Foo) session.load(test.Foo.class, fooOid);

        System.out.println("Deleting Foo");
        session.delete(foo);

        System.out.println("Flushing Session");
        session.flush();

        session.connection().commit();
        session.close();


Top
 Profile  
 
 Post subject: whoops ... itchy trigger finger ... here's the rest ...
PostPosted: Wed Nov 26, 2003 5:49 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 9:28 pm
Posts: 25
Here's the output (and exception) ...
Code:

     [java] testFooDeleteWithAnyItems()
     [java] Hibernate: insert into JPAULSEN.FOO (TEXT, OID) values (?, ?)
     [java] Hibernate: insert into JPAULSEN.ANY_ITEM (ANCHOR_CLASS, ANCHOR_OID, TEXT, OID) values (?, ?, ?, ?)
     [java] Loading Foo
     [java] Hibernate: select foo0_.OID as OID0_, foo0_.TEXT as TEXT0_ from JPAULSEN.FOO foo0_ where foo0_.OID=?
     [java] Deleting Foo
     [java] Nov 26, 2003 3:34:12 PM net.sf.hibernate.collection.PersistentCollection initialize
     [java] SEVERE: Failed to lazily initialize a collection
     [java] net.sf.hibernate.ObjectDeletedException: The object with that id was deleted: 2
     [java] Hibernate: select one_to_m0_.OID as OID__, one_to_m0_.FOO_OID as FOO_OID__, one_to_m0_.OID as OID0_, one_to_m0_.FOO_OID as FOO_OID0_, one_to_m0_.TEXT as TEXT0_ from JPAULSEN.ONE_TO_MA
NY_ITEM one_to_m0_ where one_to_m0_.FOO_OID=?
     [java]     at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2103)
     [java]     at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:2000)
     [java]     at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1926)
     [java]     at net.sf.hibernate.type.ObjectType.resolveIdentifier(ObjectType.java:124)
     [java]     at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2208)
     [java] Hibernate: select any_item0_.OID as OID__, any_item0_.ANCHOR_OID as ANCHOR_OID__, any_item0_.OID as OID0_, any_item0_.ANCHOR_CLASS as ANCHOR_C2_0_, any_item0_.ANCHOR_OID as ANCHOR_OID
0_, any_item0_.TEXT as TEXT0_ from JPAULSEN.ANY_ITEM any_item0_ where any_item0_.ANCHOR_OID=? and any_item0_.ANCHOR_CLASS = 'test.Foo'
     [java]     at net.sf.hibernate.loader.Loader.doResultSet(Loader.java:216)
     [java]     at net.sf.hibernate.loader.Loader.doFind(Loader.java:111)
     [java]     at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:722)
     [java]     at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:705)
     [java]     at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:80)
     [java]     at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:306)
     [java]     at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:3267)
     [java]     at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:199)
     [java]     at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:69)
     [java]     at net.sf.hibernate.collection.Bag.iterator(Bag.java:250)
     [java]     at net.sf.hibernate.type.PersistentCollectionType.getElementsIterator(PersistentCollectionType.java:102)
     [java]     at net.sf.hibernate.engine.Cascades.getAllElementsIterator(Cascades.java:473)
     [java]     at net.sf.hibernate.engine.Cascades.access$100(Cascades.java:27)
     [java]     at net.sf.hibernate.engine.Cascades$1.getCascadableChildrenIterator(Cascades.java:64)
     [java]     at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:428)
     [java]     at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:355)
     [java]     at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:406)
     [java]     at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:385)
     [java]     at net.sf.hibernate.impl.SessionImpl.doDelete(SessionImpl.java:1108)
     [java]     at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1050)
     [java]     at Test.testFooDeleteWithAnyItems(Test.java:90)
     [java]     at Test.main(Test.java:24)
     [java] net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
     [java]     at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:205)
     [java]     at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:69)
     [java]     at net.sf.hibernate.collection.Bag.iterator(Bag.java:250)
     [java]     at net.sf.hibernate.type.PersistentCollectionType.getElementsIterator(PersistentCollectionType.java:102)
     [java]     at net.sf.hibernate.engine.Cascades.getAllElementsIterator(Cascades.java:473)
     [java]     at net.sf.hibernate.engine.Cascades.access$100(Cascades.java:27)
     [java]     at net.sf.hibernate.engine.Cascades$1.getCascadableChildrenIterator(Cascades.java:64)
     [java]     at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:428)
     [java]     at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:355)
     [java]     at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:406)
     [java]     at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:385)
     [java]     at net.sf.hibernate.impl.SessionImpl.doDelete(SessionImpl.java:1108)
     [java]     at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1050)
     [java]     at Test.testFooDeleteWithAnyItems(Test.java:90)
     [java]     at Test.main(Test.java:24)
     [java] Caused by: net.sf.hibernate.ObjectDeletedException: The object with that id was deleted: 2
     [java]     at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2103)
     [java]     at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:2000)
     [java]     at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1926)
     [java]     at net.sf.hibernate.type.ObjectType.resolveIdentifier(ObjectType.java:124)
     [java]     at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2208)
     [java]     at net.sf.hibernate.loader.Loader.doResultSet(Loader.java:216)
     [java]     at net.sf.hibernate.loader.Loader.doFind(Loader.java:111)
     [java]     at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:722)
     [java]     at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:705)
     [java]     at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:80)
     [java]     at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:306)
     [java]     at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:3267)
     [java]     at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:199)
     [java]     ... 14 more



I'm using 2.1 beta6 and Oracle as the backend. Any insight is appreciated.

-Jay


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 9:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ah ok, this is a little wrinkle in the <any> type. I'll fix that. for a very temporary workaround change the order of the two collection mappings in the mapping file.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 11:22 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 9:28 pm
Posts: 25
Gavin,

I tried your work-around suggestion and still got the same error. Did I misunderstand the work-around? I switched the Foo.hbm.xml mapping file to specify the anyItems collection mapping before the oneToManyItems collection mapping.

-Jay


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 11:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Oh ok, it may not work, I thought it would.

I'll fix this problem with <any> today sometime.


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.