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

On Dec 29, 3:19 pm, Hudson Akridge <hudson.akri...@gmail.com> wrote:
> 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.
>
> On Tue, Dec 29, 2009 at 2:05 PM, kberridge <kevin.w.berri...@gmail.com>wrote:
>
>
>
>
>
> > 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-nhibernate@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-nhibern...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > fluent-nhibernate+unsubscr...@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-nhibern...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > fluent-nhibernate+unsubscr...@googlegroups.com<fluent-nhibernate%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-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.


Reply via email to