-->
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.  [ 11 posts ] 
Author Message
 Post subject: cascade=all-delete-orphan in a one-to-one relationship
PostPosted: Thu Jun 22, 2006 7:02 pm 
Newbie

Joined: Fri Mar 24, 2006 3:45 pm
Posts: 18
Hibernate version: 1.2.0 Alpha1

I have a one-to-one relationship between two entities, say user and address. I am using a unique foreign key mapping like this:

In User:
<many-to-one name="address" class="Address" column="AddressId" unique="true" cascade="all-delete-orphan" />

In Address:
<one-to-one name="user" class="User" property-ref="address" />

When I set user.Address to null and save the user, I would have thought that the address would be deleted from the system, but the generated sql just updates User setting its AddressId column to null.

In Hibernate In Action page 133 it says:
"all-delete-orphan [...] deletes any persistent entity instance that has been removed (derefenced) from the association ..."

I solved the problem by directly deleting the address as well as setting user.address to null but ...

1. Why do I need to directly delete the address?
2. Is there a mapping setting I'm missing? or
3. Is this a bug (as I think that it's not the expected behavior)?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 7:19 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The ref docs section 5.1.10 says "Note that single valued associations (many-to-one and one-to-one associations) do not support orphan delete." This is the expected behaviour.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 7:51 pm 
Newbie

Joined: Fri Mar 24, 2006 3:45 pm
Posts: 18
tenwit wrote:
The ref docs section 5.1.10 says "Note that single valued associations (many-to-one and one-to-one associations) do not support orphan delete." This is the expected behaviour.


OK, here are the facts:

1. NHibernate docs version 1.0.2 section 5.1.10 does NOT contain the text quoted (I checked the docs and Hibernate 2.1.7 -the version from which .NET version is ported from- doesn't have it either , its just in Hibernate 3 docs)
2. Orphan delete in one-to-one associations in effect IS NOT SUPPORTED, but
3. Many people in the Hibernate forum have argumented that orphan delete in one-to-one associations really IS THE EXPECTED BEHAVIOUR.

For now we should use session.delete(user.address) and user.address=null, but (N)Hibernate developers should strongly consider to implement the more natural and expected behaviour.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 8:09 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm told that the feature has been implemented in hibernate, though it's not in 3.0.5 (I use that version). So I would imagine that when NHibernate catches up, the feature will automagically appear.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 1:08 am 
Beginner
Beginner

Joined: Thu Oct 06, 2005 8:14 pm
Posts: 23
Really
Hibernate gurus!
which version supports this then can anyone let me know
Priya


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 1:08 am 
Beginner
Beginner

Joined: Thu Oct 06, 2005 8:14 pm
Posts: 23
Really
Hibernate gurus!
which version supports this then can anyone let me know
Priya


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 6:37 am 
Beginner
Beginner

Joined: Fri Sep 08, 2006 2:55 am
Posts: 21
Oh dear. It really would be very useful if NHibernate supported orphaning for one-to-ones, or at the least in the meantime the schema should be updated and/or NHibernate should raise an exception when you use incorrect cascade options on a <one-to-one>.

The problem for us is that so far we haven't found any solution that we can live with, which is slightly worrying.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 12:46 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
colinjack wrote:
It really would be very useful if NHibernate supported orphaning for one-to-ones ...

The problem for us is that so far we haven't found any solution that we can live with, which is slightly worrying.


I agree. How does a business object, which knows nothing about NHiberante, tell NHibernate to delete an object?

I'm still looking for options, but so far the best I can see is to dereference the object (which gives it a null property ref) and then run SQL to delete the orphans periodically.

Are there other ways of handling this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 1:51 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
pelton wrote:
I agree. How does a business object, which knows nothing about NHiberante, tell NHibernate to delete an object?

I'm still looking for options, but so far the best I can see is to dereference the object (which gives it a null property ref) and then run SQL to delete the orphans periodically.

Are there other ways of handling this?


I used a different approach. I'm not sure I really like this approach, but it is working for me.

I'm using an IInterceptor implementation which searches for objects of the type that I wanted to orphan and delete. I implemented PreFlush to check for objects of that type and then to determine if the property in question is null. If it's null I call the session.Delete method on that object.

My interceptor implementation keeps track of the session which it is intercepting.


Top
 Profile  
 
 Post subject: Use OneToMany?
PostPosted: Tue May 20, 2008 4:38 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
I'm going to try mapping it with one-to-many with logic in the POJO to only set/get one item. The primitive method will handle collections but only be used by hibernate and will never hold more than one item.

Anyone tried this? I'm hoping it will let me leverage existing delete-orphan semantics without my having to add delete logic on every update/delete call.


Top
 Profile  
 
 Post subject: Seems to have worked
PostPosted: Wed May 21, 2008 11:51 am 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
Here's the code:

Code:
    @Transient
    public Item getSingleItem() {
        if (items==null || items.isEmpty())
            return null;
        else
            return this.items.iterator().next();
    }

    /**
     * @param item
     */
    public void setItem(Item item) {
        // add the item as the (only) item.
        if (this.items==null)
            this.items= new HashSet<Item>();
        else
            this.items.clear();
       
        this.item.add(item);
    }

    /**
     * To work around the (current) inability of Hibernate to support Cascade.Delete_Orphan on a OneToOne
     * mapping, we are mapping this as one to many, with derived fields that get/set only one item.
     * This collection mapping will actually only ever hold a single item.
     */
    @OneToMany(mappedBy = "xxxx", fetch=FetchType.LAZY)
    @Cascade({CascadeType.DELETE_ORPHAN, CascadeType.ALL})
    public Set<Item> getItemsHibernateOnly() {
        return Items;
    }
   
    public void setItemsHibernateOnly(Set<Item> items) {
        this.items= items;
    }


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