Hi
I worked the whole day with this problem and I found out several things:
1. The SQL Server profiling tool only shows the sql statement and not the results. No chance.
2. The ID in the object model is set correctly to the new ID (see later). The sql statement is concatenated in a wrong way as said before.
3. I modified NHibernate (since today I am using 2.0.0) to log the returned ID, if the 'ShowSql' parameter is set to true. I changed in 'EntityIdentityInsertAction' the Execute() method:
Code:
persister.SetIdentifier(instance, generatedId, Session.EntityMode);
if (Session.Factory.Settings.IsShowSqlEnabled && instance != null && generatedId != null)
{
Console.Out.Write("NHibernate: ID returned of object " + instance.ToString() + " := ");
Console.Out.WriteLine(generatedId.ToString());
}
The if part is new to this method.
4. NHibernate 2.0.0 changed the Settings.IsShowSqlEnabled setter to internal and I changed it again to public ;)
Quote:
Why don't you create new entities instead of setting the ID of an existing entity to 0?
I wrote an undo manager which works like the "Unit Of Work Patter" of Martin Fowler to get all changes to the object model to realize an undo of operations. If an object is deleted I remove this object from the persistent objects with removing the child in the parents list of childs. So the parent does not know his child any longer but to realize an undo the child still knows his parent. This ensures that the deleted objects can be restored into the old parents list. This works fine but if I do not have a parents list (because every object in my domain can have only one parent) and the parent does not know that he has a one-to-many relation nhibernate uses the old ID. if I have a 'normal' inverse relation this work fine.
Due to the fact that we use auto generated primary keys in SQL Server 2005 it is the database part to generate valid keys and I am not able to change this behaviour. I already tried to re-attach the object with using their original id ;) but this did not work.
Does anyone know how to delete the cache of the object to force nhibernate to use the new ID of the parent?
[/quote]