Many-to-one relationship do not delete orphan children.

10 views
Skip to first unread message

Jacob Madsen

unread,
Jul 30, 2010, 9:35:48 AM7/30/10
to nhusers
Hi all,

I can't see why the orphans doesn't get delete, when cascade is set to
"all" in the "many-to-one" xml-element.

I have this mapping of an aggregate entity (table test_aggregates) and
child entity (table test_reference_entities):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="property" auto-import="true" default-cascade="save-update"
default-lazy="false">
<class xmlns="urn:nhibernate-mapping-2.2" lazy="false"
mutable="true" name="TestAggregate, Tests, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" table="test_aggregates">
<id access="backfield" name="Id" type="System.Int64, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="id" />
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
<many-to-one cascade="all"
class="Namic.Being.Framework.Tests.Database.TestReferenceEntity,
Namic.Being.Framework.Tests, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" name="ReferenceEntity">
<column name="ReferenceEntity_id" />
</many-to-one>
</class>
</hibernate-mapping>


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="property" auto-import="true" default-cascade="save-update"
default-lazy="false">
<class xmlns="urn:nhibernate-mapping-2.2" lazy="false"
mutable="true" name="TestReferenceEntity, Tests, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" table="test_reference_entities">
<id access="backfield" name="Id" type="System.Int64, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="id" />
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
</class>
</hibernate-mapping>


Cheers!

Aaron Fischer

unread,
Jul 30, 2010, 4:09:01 PM7/30/10
to nhusers
cascade="all-delete-orphan"

Sergej Koščejev

unread,
Jul 30, 2010, 4:41:03 PM7/30/10
to nhu...@googlegroups.com
On 30.7.2010 22:09, Aaron Fischer wrote:
> cascade="all-delete-orphan"
>
As far as I remember, orphan delete is only supported for collections.

Sergej

Jacob Madsen

unread,
Aug 1, 2010, 4:16:23 PM8/1/10
to nhusers
Yes, "all-delete-orphan" is not an option.

Do someone know if delete orphan is even supported for many-to-one?

I am using version 2.1.2.4000.

Diego Mijelshon

unread,
Aug 1, 2010, 10:14:37 PM8/1/10
to nhu...@googlegroups.com
Many-to-one doesn't have the concept of orphans (it doesn't make sense).
Valid cascade values are here: http://nhforge.org/doc/nh/en/index.html#mapping-declaration-manytoone

 
    Diego


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Jacob Madsen

unread,
Aug 2, 2010, 3:38:21 AM8/2/10
to nhu...@googlegroups.com
Are you sure it doesn't make sense? Do you believe that its not be possible in real world domains that (many-to-one) child entities is replaced in the lifetime of the aggregate.



2010/8/2 Diego Mijelshon <di...@mijelshon.com.ar>

Jacob Madsen

unread,
Aug 2, 2010, 3:47:39 AM8/2/10
to nhusers
Let me add this to the previous mail... I don't believe in allowing
nhibernate (infrastructure) within the borders of an aggregate.


On Aug 2, 9:38 am, Jacob Madsen <ja...@mungo.dk> wrote:
> Are you sure it doesn't make sense? Do you believe that its not be possible
> in real world domains that (many-to-one) child entities is replaced in the
> lifetime of the aggregate.
>
> 2010/8/2 Diego Mijelshon <di...@mijelshon.com.ar>
>
>
>
> > Many-to-one doesn't have the concept of orphans (it doesn't make sense).
> > Valid cascade values are here:
> >http://nhforge.org/doc/nh/en/index.html#mapping-declaration-manytoone
> >  <http://nhforge.org/doc/nh/en/index.html#mapping-declaration-manytoone>
>
> >     Diego
>
> > On Sun, Aug 1, 2010 at 17:16, Jacob Madsen <ja...@mungo.dk> wrote:
>
> >> Yes, "all-delete-orphan" is not an option.
>
> >> Do someone know if delete orphan is even supported for many-to-one?
>
> >> I am using version 2.1.2.4000.
>
> >> On Jul 30, 10:41 pm, Sergej Koščejev <sergey....@gmail.com> wrote:
> >> >   On 30.7.2010 22:09, Aaron Fischer wrote:> cascade="all-delete-orphan"
>
> >> > As far as I remember, orphan delete is only supported for collections.
>
> >> > Sergej
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "nhusers" group.
> >> To post to this group, send email to nhu...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/nhusers?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >
> > .

Diego Mijelshon

unread,
Aug 2, 2010, 8:06:31 AM8/2/10
to nhu...@googlegroups.com
I didn't understand your statement, but many-to-one is a parent-to-child relationship (i.e. the declaring entity depends on the referenced one).

Let's say I have three classes, Parent, ChildA and ChildB.
ChildA and ChildB have many-to-one references to Parent.
When setting a ChildA.Parent to null, how in the world do you expect to check if the Parent is "orphan"? By doing two selects (in ChildA and ChildB tables) and seeing if there are any records???
 
    Diego

Diego Mijelshon

unread,
Aug 2, 2010, 8:21:45 AM8/2/10
to nhu...@googlegroups.com
Read: many-to-one is a child-to-parent relationship
 
    Diego

Frans Bouma

unread,
Aug 2, 2010, 8:52:59 AM8/2/10
to nhu...@googlegroups.com
> Read: many-to-one is a child-to-parent relationship

I think calling it a parent-child relationship is not really helpful, it's more of a A-depends-on-B relationship, i.o.w.: it's a dependency, not a parent -> child relationship, as for example 'Order' in northwind has 2 m:1 relationships: with Customer and with Employee. Which one is the 'parent' ? Also in diamond shaped models, multiple dependency paths are visible, but on the node level it's not known which path is the real dependency.

FB

>
> Diego
>
>
>
> On Mon, Aug 2, 2010 at 09:06, Diego Mijelshon <di...@mijelshon.com.ar>
> wrote:
>
>
> I didn't understand your statement, but many-to-one is a parent-to-
> child relationship (i.e. the declaring entity depends on the referenced
> one).
>
> Let's say I have three classes, Parent, ChildA and ChildB.
> ChildA and ChildB have many-to-one references to Parent.
> When setting a ChildA.Parent to null, how in the world do you expect
> to check if the Parent is "orphan"? By doing two selects (in ChildA and
> ChildB tables) and seeing if there are any records???
>
> Diego
>
>
>
> On Mon, Aug 2, 2010 at 04:38, Jacob Madsen <ja...@mungo.dk> wrote:
>
>
> Are you sure it doesn't make sense? Do you believe that its
> not be possible in real world domains that (many-to-one) child entities is
> replaced in the lifetime of the aggregate.
>
>
>
> 2010/8/2 Diego Mijelshon <di...@mijelshon.com.ar>
>
>
> Many-to-one doesn't have the concept of orphans (it
> doesn't make sense).
> Valid cascade values are here:
> http://nhforge.org/doc/nh/en/index.html#mapping-declaration-manytoone

> <http://nhforge.org/doc/nh/en/index.html#mapping-
> declaration-manytoone>


>
> Diego
>
>
>
> On Sun, Aug 1, 2010 at 17:16, Jacob Madsen
> <ja...@mungo.dk> wrote:
>
>
> Yes, "all-delete-orphan" is not an option.
>
> Do someone know if delete orphan is even
> supported for many-to-one?
>
> I am using version 2.1.2.4000.
>
>
>
> On Jul 30, 10:41 pm, Sergej Koščejev
> <sergey....@gmail.com> wrote:
> > On 30.7.2010 22:09, Aaron Fischer wrote:>
> cascade="all-delete-orphan"
> >
> > As far as I remember, orphan delete is only
> supported for collections.
> >
> > Sergej
>
> --
> You received this message because you are
> subscribed to the Google Groups "nhusers" group.
> To post to this group, send email to
> nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com

> <mailto:nhusers%2Bunsu...@googlegroups.com> .


> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
>
>
>
>
> --
>
> You received this message because you are subscribed to
> the Google Groups "nhusers" group.
> To post to this group, send email to
> nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com

> <mailto:nhusers%2Bunsu...@googlegroups.com> .


> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
>
>
>
> --
>
> You received this message because you are subscribed to the
> Google Groups "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com

> <mailto:nhusers%2Bunsu...@googlegroups.com> .

Diego Mijelshon

unread,
Aug 2, 2010, 9:07:13 AM8/2/10
to nhu...@googlegroups.com
Nitpicker ;-)

Point taken. 
he logic around it is still the same, regardless of how we call it. If A references/depends-on/is-a-child-of B, then it makes no sense to orphan B based on its relationship with A.
 
    Diego


2010/8/2 Frans Bouma <fr...@sd.nl>

Frans Bouma

unread,
Aug 2, 2010, 9:23:24 AM8/2/10
to nhu...@googlegroups.com
> Nitpicker ;-)

:P

In ORM land, parent-child is already defined: for inheritance, so IMHO it's best not to overload it with an ambiguistic definition again. But it's hard though, many people still think that an FK constraint defines a parent-child relationship ;)

> Point taken.
> he logic around it is still the same, regardless of how we call it. If A
> references/depends-on/is-a-child-of B, then it makes no sense to orphan B
> based on its relationship with A.

indeed, as B is never an 'orphan' in the sense that it has a violating constraint. So I think the topic starter wants something else than what he discussed.

FB

> > <mailto:nhusers%2Bunsu...@googlegroups.com
> <mailto:nhusers%252Buns...@googlegroups.com> > .


>
> > For more options, visit this group at
> > http://groups.google.com/group/nhusers?hl=en.
> >
> >
> >
> >
> >
> >
> > --
> >
> > You received this message because you are
> subscribed to
> > the Google Groups "nhusers" group.
> > To post to this group, send email to
> > nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com
> <mailto:nhusers%2Bunsu...@googlegroups.com>
>

> > <mailto:nhusers%2Bunsu...@googlegroups.com
> <mailto:nhusers%252Buns...@googlegroups.com> > .


>
> > For more options, visit this group at
> > http://groups.google.com/group/nhusers?hl=en.
> >
> >
> >
> >
> >
> > --
> >
> > You received this message because you are subscribed
> to the
> > Google Groups "nhusers" group.
> > To post to this group, send email to
> nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com
> <mailto:nhusers%2Bunsu...@googlegroups.com>
>

> > <mailto:nhusers%2Bunsu...@googlegroups.com
> <mailto:nhusers%252Buns...@googlegroups.com> > .

Jacob Madsen

unread,
Aug 2, 2010, 3:08:48 PM8/2/10
to nhu...@googlegroups.com
It seems that I have misused the term "orphan" in context of nhibernate.

Imagine that I have (please ignore lazy load/virtual and so on):

public class A
{
    private B _b;

    public A()
    {
        _b = new B();
    }

    public void ChangeB(string input)
    {
        var b = new B(input);

        _b = b;
    }
}

How would you delete the old (what I called orphan before) _b from the database, when nhibernate / infrastructure should not be part of the aggregate?

Diego Mijelshon

unread,
Aug 2, 2010, 4:16:11 PM8/2/10
to nhu...@googlegroups.com
Use a trigger.
 
    Diego

Jacob Madsen

unread,
Aug 2, 2010, 5:33:38 PM8/2/10
to nhu...@googlegroups.com
Is there a nhibernate abstraction to triggers or will triggers force me to implicit depend on a specific database product?

Fabio Maulo

unread,
Aug 2, 2010, 5:35:50 PM8/2/10
to nhu...@googlegroups.com
database-object
Fabio Maulo

Jacob Madsen

unread,
Aug 2, 2010, 5:47:40 PM8/2/10
to nhu...@googlegroups.com
Hi Fabio,

I'm a bit unsure of what you mean by "database-object"?

Diego Mijelshon

unread,
Aug 2, 2010, 5:55:25 PM8/2/10
to nhu...@googlegroups.com

Fabio Maulo

unread,
Aug 2, 2010, 5:56:43 PM8/2/10
to nhu...@googlegroups.com
http://nhforge.org/doc/nh/en/index.html#mapping-database-object
You can create triggers and define the dialect-scope

2010/8/2 Jacob Madsen <ja...@mungo.dk>



--
Fabio Maulo

Jacob Madsen

unread,
Aug 2, 2010, 6:08:14 PM8/2/10
to nhu...@googlegroups.com
Great help guys!

jimbobmcgee

unread,
Aug 2, 2010, 5:41:58 PM8/2/10
to nhusers
A trigger will be part of your DDL; it is a database object that you
will create after creating your table.
So yes, it may be required to be different depending on the RDBM that
you use, but not much more so than your CREATE TABLE statements...
> >>> >       > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> >>> > <mailto:nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>
>
> >>> >       > <mailto:nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>
> >>> > <mailto:nhusers%252Buns...@googlegroups.com<nhusers%25252Bun...@googlegroups.com>>
> >>> > .
>
> >>> >       >                               For more options, visit this
> >>> group at
> >>> >       >http://groups.google.com/group/nhusers?hl=en.
>
> >>> >       >                       --
>
> >>> >       >                       You received this message because you are
> >>> > subscribed to
> >>> >       > the Google Groups "nhusers" group.
> >>> >       >                       To post to this group, send email to
> >>> >       > nhu...@googlegroups.com.
> >>> >       >                       To unsubscribe from this group, send
> >>> email to
> >>> >       > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> >>> > <mailto:nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>
>
> >>> >       > <mailto:nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>
> >>> > <mailto:nhusers%252Buns...@googlegroups.com<nhusers%25252Bun...@googlegroups.com>>
> >>> > .
>
> >>> >       >                       For more options, visit this group at
> >>> >       >http://groups.google.com/group/nhusers?hl=en.
>
> >>> >       >               --
>
> >>> >       >               You received this message because you are
> >>> subscribed
> >>> > to the
> >>> >       > Google Groups "nhusers" group.
> >>> >       >               To post to this group, send email to
> >>> > nhu...@googlegroups.com.
> >>> >       >               To unsubscribe from this group, send email to
> >>> >       > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> >>> > <mailto:nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>
>
> >>> >       > <mailto:nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>
> >>> > <mailto:nhusers%252Buns...@googlegroups.com<nhusers%25252Bun...@googlegroups.com>>
> >>> > .
>
> >>> >       >               For more options, visit this group at
> >>> >       >http://groups.google.com/group/nhusers?hl=en.
>
> >>> >       > --
> >>> >       > You received this message because you are subscribed to the
> >>> Google
> >>> > Groups
> >>> >       > "nhusers" group.
> >>> >       > To post to this group, send email to nhu...@googlegroups.com.
> >>> >       > To unsubscribe from this group, send email to
> >>> >       > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> >>> > <mailto:nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>>
> >>> .
> >>> >       > For more options, visit this group at
> >>> >       >http://groups.google.com/group/nhusers?hl=en.
>
> >>> >       --
> >>> >       You received this message because you are subscribed to the
> >>> Google
> >>> > Groups "nhusers" group.
> >>> >       To post to this group, send email to nhu...@googlegroups.com.
> >>> >       To unsubscribe from this group, send email to
> >>> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> >>> > <mailto:nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>>
> >>> .
> >>> >       For more options, visit this group at
> >>> >http://groups.google.com/group/nhusers?hl=en.
>
> >>> > --
> >>> > You received this message because you are subscribed to the Google
> >>> Groups
> >>> > "nhusers" group.
> >>> > To post to this group, send email to nhu...@googlegroups.com.
> >>> > To unsubscribe from this group, send email to
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages