HasMany and Not.Nullable

447 views
Skip to first unread message

kberridge

unread,
Dec 29, 2009, 2:37:15 PM12/29/09
to Fluent NHibernate
I have a simple parent child relationship where the parent class has a
collection of children, but the child class doesn't have a reference
to the parent. I'm mapping it with a HasMany.

In my database, the foreign key column does not allow nulls, but when
NH tries to insert into these tables it is bombing out because it's
trying to insert the child first (with null in the FK col) and come
back and update the FK column after it inserts the parent.

I found an earlier message on this group that briefly discusses this
issue: http://www.mail-archive.com/fluent-n...@googlegroups.com/msg05124.html

Is the only way I can get around this problem still to add the
reference to the parent on the child class?

Thanks,
Kevin Berridge

Eric Ridgeway

unread,
Dec 29, 2009, 2:58:31 PM12/29/09
to fluent-n...@googlegroups.com

Try setting inverse on the has many mapping and see if that helps


--

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


kberridge

unread,
Dec 29, 2009, 3:05:56 PM12/29/09
to Fluent NHibernate
Nope, didn't change the behavior.

Thanks,
Kevin

On Dec 29, 2:58 pm, Eric Ridgeway <ang3lf...@gmail.com> wrote:
> Try setting inverse on the has many mapping and see if that helps
>

> On Dec 29, 2009 11:54 AM, "kberridge" <kevin.w.berri...@gmail.com> wrote:
>
> I have a simple parent child relationship where the parent class has a
> collection of children, but the child class doesn't have a reference
> to the parent.  I'm mapping it with a HasMany.
>
> In my database, the foreign key column does not allow nulls, but when
> NH tries to insert into these tables it is bombing out because it's
> trying to insert the child first (with null in the FK col) and come
> back and update the FK column after it inserts the parent.
>
> I found an earlier message on this group that briefly discusses this

> issue:http://www.mail-archive.com/fluent-n...@googlegroups.com/msg051...


>
> Is the only way I can get around this problem still to add the
> reference to the parent on the child class?
>
> Thanks,
> Kevin Berridge
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Fluent NHibernate" group.
> To post to this group, send email to fluent-n...@googlegroups.com.
> To unsubscribe from this group, send email to

> fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsubscr i...@googlegroups.com>

Hudson Akridge

unread,
Dec 29, 2009, 3:19:57 PM12/29/09
to fluent-n...@googlegroups.com
Inverse is only required if it's got a bi-directional back. Inverse at this point would be the opposite of what we'd want, we want to declare the collection side to be the parent.

The way a typical HasMany association works is that it's the child side that saves the reference back to the parent, so there's an insert that gets fired to insert the child, then an update on the child side that updates the parent with the key reference.

Failing that, you may actually be wanting to do a HasMany() with a composite element, rather than map the child in a separate mapping without a parent. That is how a true parent/child works, the child cannot exist without the parent.

If you want the child to exist without the parent then it needs to have a reference back to the parent, otherwise you will encounter a situation where the child attempts to be deleted, but it can't be because of the FK violation to the parent, because the parent hasn't been told the child needs to be deleted out of the collection, and so you'll get a failure in NH stating that an object you're attempting to delete will be re-saved via a cascade.

To sum:
Child exist without parent - Bi-Directional
Child exist only with parent - composite element or bi-directional

 To quote the NHibernate documentation, 6.4:
Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter.

That's your scenario problem explained.

To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.


kberridge

unread,
Dec 29, 2009, 4:53:57 PM12/29/09
to Fluent NHibernate
I found an article on what you suggest with doing a HasMany() with a
composite element:
http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/08/15/a-fluent-interface-to-nhibernate-part-3-mapping.aspx

It uses a method called HasManyComponent which doesn't seem to exist
in the current release of Fluent. But this is an old article. Is
there a new way to do this?

As you guessed, I want my child to not exist without a parent. And I
want to be able to cause the child to be inserted by simply adding it
into the Parent's collection, since that most closely represents that
requirement. Am I on the right track here?

Thanks,
Kevin

> > > fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsubscr i...@googlegroups.com><fluent-nhibernate%2Bunsubscr


> > i...@googlegroups.com>
> > > .
> > > For more options, visit this group athttp://
> > groups.google.com/group/fluent-nhibernate?hl=en.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Fluent NHibernate" group.
> > To post to this group, send email to fluent-n...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsubscr i...@googlegroups.com>
> > .

Hudson Akridge

unread,
Dec 29, 2009, 6:08:25 PM12/29/09
to fluent-n...@googlegroups.com
You're on the right track. Composite element in a collection should
work for your needs. I don't recall off the top of my head what the
fnh syntax is, but look at something like .hasmany().ascompositeelement
() or something like that. (I'm on my mobile atm otherwise I'd look it
up for you)

On Dec 29, 2009, at 3:53 PM, kberridge <kevin.w....@gmail.com>
wrote:

>>> %2Bunsubscr i...@googlegroups.com>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/fluent-nhibernate?hl=en.
>>
>> --
>> - Hudsonhttp://www.bestguesstheory.comhttp://twitter.com/
>> HudsonAkridge
>

> --
>
> You received this message because you are subscribed to the Google
> Groups "Fluent NHibernate" group.
> To post to this group, send email to fluent-n...@googlegroups.com
> .
> To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com

kberridge

unread,
Dec 29, 2009, 6:20:47 PM12/29/09
to Fluent NHibernate
Thanks. I just found HasMany( x => x.Children ).Component( ... ).
I'll try it out!

Thanks again,
Kevin

On Dec 29, 6:08 pm, Hudson Akridge <hudson.akri...@gmail.com> wrote:
> You're on the right track. Composite element in a collection should  
> work for your needs. I don't recall off the top of my head what the  
> fnh syntax is, but look at something like .hasmany().ascompositeelement
> () or something like that. (I'm on my mobile atm otherwise I'd look it  
> up for you)
>

> On Dec 29, 2009, at 3:53 PM, kberridge <kevin.w.berri...@gmail.com>  


> wrote:
>
>
>
> > I found an article on what you suggest with doing a HasMany() with a
> > composite element:

> >http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/08/15/a-fl...

Reply all
Reply to author
Forward
0 new messages