-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem removing item from collection and then saving
PostPosted: Wed Feb 08, 2006 1:46 pm 
Newbie

Joined: Fri Sep 09, 2005 11:40 am
Posts: 12
This represents the structure I have not the data or tables.

Hibernate version: 3.0

Mapping documents:
Object A map:
Code:
<id name="shelfId" column="shelf_id"
      <generator class="native"/>
</id>

....

<bag name="bookInfo" table="books" inverse="false" cascade="save-update">
     <key column="shelf_id"/>
     <one-to-many class="BookInfo"/>
</bag>
             



Object B map:

Code:
...
     <composite-id>
           <key-property name="shelfId" column="shelf_id" />
           <key-property name="bookId" column="book_id"/>
     </composite-id>

     <version name="version" column="version" unsaved-value="null"/>
...


Object A object:

Code:
...
    private Long shelfId;
    private ArrayList objB
...


Object B object:

Code:
...   
    private Long shelfId;
    private Long bookId;
...





In my application I can modify the list of object B items contained by object A including adding and removing elements to/from the list. After the elements in the arraylist have been modified to satisfaction, the user is able to persist their changes to the database.

If the user deletes an element from the ArrayList, I call the remove method on the array list removing the element from the list and place the object in another list that is used to actually delete the object from the database. All of this seems to work fine EXCEPT when object B has a complex id as above.

Adding elements to B and calling save on object A works fine with the complex id.

When the user chooses to delete an element, I remove the object from the arraylist and build my delete list of objects to be deleted from the database. When it is time to save the data to the database I do the following:
Code:
          objectA.saveOrUpdate();
     


This fails if I have removed objects from the arraylist of object B.

I do have a hashCode and equals methods in object B.

I do this same thing on other objects that do not have a complex id and everything works fine. Unfortunately I do not have the luxury of modifying the database to do away with the complex id.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 2:17 pm 
Newbie

Joined: Wed Jan 28, 2004 5:18 am
Posts: 11
Quote:
This fails if I have removed objects from the arraylist of object B.

How exactly does it fail?
When you do saveOrUpdate for A, Hibernate will try to set shelfId to null in object B that you deleted (since you have cascade to save-update).
Since shelfId is part of a composite id, this might break database consistency (PK unique constraint).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 4:01 pm 
Newbie

Joined: Fri Sep 09, 2005 11:40 am
Posts: 12
gcata wrote:
Quote:
This fails if I have removed objects from the arraylist of object B.

How exactly does it fail?
When you do saveOrUpdate for A, Hibernate will try to set shelfId to null in object B that you deleted (since you have cascade to save-update).
Since shelfId is part of a composite id, this might break database consistency (PK unique constraint).


The SQL generated by Hibernate during the saveOrUpdate is essentially trying to set the shelfId to null as you said. Ahhh, if I turn off cascading then I should be okay. I think maybe I have misunderstood the cascade attribute. I have been thinking that setting cascade on for save/update meant that in this case if object A is updated then object B would also be update.

If this is incorrect then will a modification to object B still be recognized if cascade is set to none for the association in A??

Going to try this now.


Michelle


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 10:39 am 
Newbie

Joined: Wed Jan 28, 2004 5:18 am
Posts: 11
Quote:
Ahhh, if I turn off cascading then I should be okay.

Actually, no.
Hibernate is not setting the field to null because of the cascade="save/update" attribute, but because of the "one-to-many" mapping - this is how it works.
I mentioned the cascade attribute because you can use it to automatically delete the entity B when you remove it from A (by using parent-child mapping, with cascade=all-delete-orphan), and this way you would avoid this problem.


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