-->
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: Problems with lazy inverse collection in hibernate 3.2.1
PostPosted: Thu Jan 04, 2007 11:06 am 
Newbie

Joined: Thu Jan 04, 2007 10:45 am
Posts: 1
I load an Object that contains a lazy inverse List (one-to-many).

My collection is wrapped by a PersistentMap

There is objects in the collection in the database but in my case the PersistentMap is not yet initialized.

I perform a remove operation on the persistentMap with a known key of one of the objects.

The map is not initialized (and the relation is an inverse one-to-many) so the map queue the removeOperation.

I perform a get operation with the same key and the value is still returned.

If we look closer at what happened in the PersistentMap, it's look like this.
Code:
public Object remove(Object key) {
  if ( isPutQueueEnabled() ) {                            // This returned true, the
                                                          // map is not yet                     
                                                          // initialized

     Object old = readElementByIndex( key );     // This method triggered
                                                 // an initialization of the                   
                                                 // map.
                                                 // Queued operation are
                                                 // processed in the after
                                                 // init method

     queueOperation( new Remove( key, old ) );  // The remove operation   
                                                // is queued.
     return old;
   }
   ....



When i perform the get operation on the map the map is now initialized. The get is processed by the underlying map and the value is returned. The queued operation is completely ignored.

Im i doing something wrong?
Currently i fixed my code by performing a containsKey before the remove . The containsKey initialize the map and then the remove do not queue the operation. But by looking at all the PersistentCollection it seem that i may have the same problem using put and remove with other persistentCollection.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 11:05 pm 
Newbie

Joined: Sun Apr 22, 2007 5:05 pm
Posts: 1
I believe this issue still exists in Hibernate 3.2.3.ga.

Here is a sample test case which demonstrates this issue. This test case can be placed into the file test/org/hibernate/test/collection/map/PersistentMapTest.java in the Hibernate source distribution:

Code:
public void testRemoveAgainstUninitializedMap() {
    Parent parent = new Parent( "p1" );
    Child child = new Child( "c1" );
    parent.addChild(child);

    Session session = openSession();
    session.beginTransaction();
    session.save( parent );
    session.getTransaction().commit();
    session.close();

    // Now reload the parent and test removing the child
    session = openSession();
    session.beginTransaction();
    parent = ( Parent ) session.get( Parent.class, parent.getName() );
    Map children = parent.getChildren();
    children.remove(child.getName());
    session.getTransaction().commit();
    session.close();

    assertTrue( parent.getChildren().isEmpty() );

    session = openSession();
    session.beginTransaction();
    session.delete( parent );
    session.getTransaction().commit();
    session.close();
}


Note that this example requires a modification to test/org/hibernate/test/collection/map/Mappings.hbm.xml to give a formula attribute to the Parent's map of child name to Child objects. (Otherwise, you get errors when trying to read values from an indexed collection with a null index key).

Code:
<map name="children" inverse="true" cascade="all">
  <key column="PARENT" />
  <map-key type="string" formula="NAME"/>
  <one-to-many class="Child" />
</map>



I have submitted a proposed patch with this test case at HHH-2584. I followed a similar patch style as was done for HHH-2476.

I'm curious to see if this patch would solve your issue. While I tried to follow the same style as a previous patch, I may have missed something. Any feedback or advice is appreciated.

Cheers,
Dan


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.