@PeteA.

Yes, the index is part of the primary key. the database terms, Child is 
what is called in database terms a dependent entity. Yes, children could be 
deleted, but the only assumption you can make is that, for a given parent, 
the Index is unique among children. In this particular case the order 
doesn't matter.

I guess in my scenario, NH doesn't know whether the child object it's a new 
one or not, and that's acceptable, even though logically it could mark the 
children as new because their parent is new (which NH should not have a 
problem detecting).

>From a database modeling point of view,  it doesn't always make sense to 
assign surrogate primary keys to dependent entities. The example that I 
gave was simplified. In the real world the children of the parent are 
description records in different languages of the parent. Instead of the 
Index field I have a language code/country code, so the child key is made 
of parentId/language code/country code. The language code and the country 
code are Foreign Keys to a table that contains Cultures. 

I still think that NH can mark the object as new if one of the FK objects 
that is also PK field is new.

Thanks



On Monday, November 26, 2012 1:44:32 AM UTC-8, PeteA wrote:
>
> I suspect that you've not told NHibernate how to know that it's a new 
> child (by way of the unsaved-value attribute.  However, is it really true 
> that the index is part of the child's PK as opposed to a UQ?  My reading of 
> the pseudo-code is that you've got a parent with an ordered list of 
> children; is the order guaranteed immutable (including no deletes, ever!) 
> and truly an identifier?  If you ever delete or re-order children then I'd 
> suggest that the child is promoted to a 'first class' entity with it's own 
> ID.  FWIW, when I have this situation my pattern tends to be:
>
>  
>
> TChild:
>
> Parent {get;protected set;}
>
> Index { get { return Parent.Children.IndexOf(this); } }
>
> Child(Parent parent) {
>
> if(parent == null) throw;
>
> }
>
>  
>
> /Pete
>
>  
>
>  
>
> *From:* [email protected] <javascript:> [mailto:
> [email protected] <javascript:>] *On Behalf Of *costa
> *Sent:* 23 November 2012 19:27
> *To:* [email protected] <javascript:>
> *Subject:* [nhusers] Re: How to save new entities with generated Guid 
> without requiring a select from NH
>
>  
>
> Hello:
>
> I have the same problem. I have two tables TParent/TChild where TChild is 
> a dependent table, i.e.
>
> TParent:
> id (PK) - identity field (I am using Sql Server 2008)
> name
>
> TChild
> ParentId (PK1) -> TParent.Id
> Index  (PK2)
> ...
>
> The combination ParentId/Index is unique.
>
>
>
> When I save the entities:
> I have something like this:
>
> var parent = new TParent();
> var child = new TChild();
> child.Index = 1;
> child.Parent = parent;
>
> parent.Children.Add(child);
>
> child = new TChild();
> child.Index = 2;
> child.Parent = parent;
>
> parent.Children.Add(child);
>
> Session.SaveOrUpdate(parent);
>
> When this runs, I get the warning:
>
>  Unable to determine if ... with assigned identifier is transient or 
> detached
>
> I run a sql trace and before NH executes the child inserts it executes 
> select statements to see if the children are there.
>
> I am still using NH 3.1.0. I will upgrade to 3.3.1 soon, but I was curious 
> why NH can't figure out that the children are new.
>
>
> Calling Session.Save(child) before saving the parent (and right after the 
> child was added to the Children collection) doesn't seem to work because it 
> fired the child inserts before the parent was created
>
>
> Thanks
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "nhusers" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/nhusers/-/ygu-KT2PgY4J.
> To post to this group, send email to [email protected] <javascript:>
> .
> To unsubscribe from this group, send email to 
> [email protected] <javascript:>.
> 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 view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/gA9HbYDZSDwJ.
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.

Reply via email to