NHibernate parent-childs save redundant sql update executed

痞子三分冷 提交于 2019-12-25 16:40:01

问题


I'm trying to save (insert) parent object with a collection of child objects, all objects are new. I prefer to manually specify what to save\update and when so I do not use any cascade saves in mappings and flush sessions by myself. So basically I save this object graph like:

session.Save(Parent)
foreach (var child in Parent.Childs)
{
 session.Save(child);
}
session.Flush()

I expect this code to insert Parent row, then each child row, however NHibernate executes this SQL:

INSERT INTO PARENT....
INSERT INTO CHILD ....
UPDATE CHILD SET ParentId=@1 WHERE Id=@2 //What the point of update if ParentId was set in previous query

This update statement is absolutely unnecessary, ParentId was already set correctly in INSERT. How do I get rid of it? Performance is very important for me.


回答1:


I forgot "Inverse" in mappings on many-to-one collection.



来源:https://stackoverflow.com/questions/4539872/nhibernate-parent-childs-save-redundant-sql-update-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!