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: How to delete orphan children
PostPosted: Fri Sep 17, 2004 12:10 pm 
Newbie

Joined: Mon Sep 13, 2004 2:23 pm
Posts: 6
Location: Los Angeles
Hibernate version: 2

I've got a list of child objects (keywords) that I'd like to store in a separate table. My configuration is as such (it's xdoclet, but I'm sure you can see the gist of it):

/**
* Returns the keyword list associated with the referenced physical asset.
*
* @hibernate.set table="ImageMaster_Keywords" lazy="false" cascade="all-delete-orphan"
* @hibernate.collection-key column="imagemaster_fk"
* @hibernate.collection-one-to-many class="com.enetworks.style2.ssms.common.dao.Keyword"
* @return List of keywords
*/

public Set getKeywords() {
return keywords;
}

I've used all-delete-orphan for the cascade definition since I want all actions to be cascaded from the parent to the child, and in particular I'd like orphan children to be deleted.

Well, it's not happening - one of my tests creates the parent object, sets a bunch of keywords, then saves the object in the database. Then it retrieves the same object and removes some of the keywords and stores it again. Now, the old keywords that were removed are still in the database - the PK is set to null, which makes sense, but that's behavior I would expect with all or save-update and not with all-delete-orphan. Does anyone know what I need to do to make this work?

_________________
The problems that exist in the world today cannot be solved by the level of thinking that created them -- Albert Einstein (some smart dude)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 2:43 pm 
Regular
Regular

Joined: Wed Jun 30, 2004 4:02 pm
Posts: 64
I am having the exact same problem you are. Page 133 of Hibernate in Action describes the behavior of all-delete-orphan as what we expect, but it is not working that way. Hibernate tries to update the record and set the foreign key to null, instead of deleteing. I am using 2.1.6. and Oracle 9i.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 8:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
just remove from the collection, don't set the parent ref to null


(and please don't post the same problem in multiple threads)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 9:42 pm 
Regular
Regular

Joined: Wed Jun 30, 2004 4:02 pm
Posts: 64
I do not set a parent ref to null. All I do is:

Code:

        Set userSalesRegions = editedUser.getSalesRegions();
        if (userSalesRegions == null) {
            userSalesRegions = new HashSet();
        }
if (!userSalesRegions.isEmpty()) {
            // Revoke the specified regions
            for (int i = 0; i < unassignedItems.length; i++) {
                String strRevokeRegionId = unassignedItems[i];
                int revokeRegionId = Integer.parseInt(strRevokeRegionId);
                SalesRegion revokedRegion = lookupManager.getSalesRegion(revokeRegionId);

                Iterator iterator = userSalesRegions.iterator();
                while (iterator.hasNext()) {
                    UserSalesRegion priorGrantedRegion = (UserSalesRegion) iterator.next();

                    if (revokedRegion.getId().equals(priorGrantedRegion.getSalesRegion().getId())) {

                        log.info("They were equal.  Revoking now");

                        iterator.remove();
                    }
                }
            }
        }


After this I just pass the editedUser object to the service layer's save, which leads to a hibernate saveOrUpdate(editedUser);

Hibernate will try to set the fk to null instead of doing a delete. I do have a composite primary key on the child object, and other threads have hinted that this will not work due to a hibernate limitation.

Any insight?


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.