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] [mailto:[email protected]] On
Behalf Of costa
Sent: 23 November 2012 19:27
To: [email protected]
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].
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.
--
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.