Hi Saijado,
This is by NHibernate design. If you want to prevent a null-insert, then I
think you need to use a bi-directional association:
http://nhforge.org/doc/nh/en/index.html#collections-onetomany
>From the docs:
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.
Hope that helps.
Regards,
Richard
--------------------------------------------------
From: "Saijado" <[email protected]>
Sent: Friday, July 31, 2009 9:41 AM
To: "nhusers" <[email protected]>
Subject: [nhusers] Unecessary update in one-to-many and problems with
foreign keys on the database
>
> Hallo,
>
> I have the following class structure:
>
> class Parent{
> long Id{get;set;}
> IList<Child> Children{get;set}
> }
>
> class Child{
> long Id{get;set;}
> }
>
> If I'm doing somthing like this:
>
> var parent = new Parent();
> parent.Children.Add(new Child());
> session.Save(parent);
> session.Flush();
>
> It will produce the insert for children at first with the referencing
> id to the parent as null. Then it inserts the parent and then updates
> the referencing id's in the children.
>
> But why? The correct way should be insert the parent and then insert
> the children and setting the id to the parent directly. Unlikly this
> causes sometimes Problems in our database, becaus there are foreign
> key constraints definded.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---