Nhibernate 有很多插入会生成额外的更新(无反向)
我有一个包含两个子类列表的父类
public class Parent
{
...
public virtual ICollection<Foo> Foo{ get; set; }
public virtual ICollection<Bae> Bar{ get; set; }
}
public class Foo
{
...
public virtual Parent Parent{ get; set; }
}
public class Bar
{
...
public virtual Parent Parent{ get; set; }
}
映射是
public ParentMap()
{
...
HasMany(m => m.Foo).AsSet().Cascade.All();
HasMany(m => m.Bar).AsSet().Cascade.All();
}
public FooMap()
{
...
References(m => m.Parent);
}
public BarMap()
{
...
References(m => m.Parent);
}
每当我保存父对象时我得到
INSERT Parent ...
INSERT Foo with Parent_id set to NULL
INSERT Bar with Parent_id set to NULL
UPDATE Foo set Parent_id to Parent id
UPDATE Bar set Parent_id to Parent id
是否有一种方法分开将关系设置为Inverse
以避免额外更新?
I have a Parent class with two lists of Child classes
public class Parent
{
...
public virtual ICollection<Foo> Foo{ get; set; }
public virtual ICollection<Bae> Bar{ get; set; }
}
public class Foo
{
...
public virtual Parent Parent{ get; set; }
}
public class Bar
{
...
public virtual Parent Parent{ get; set; }
}
The mappings are
public ParentMap()
{
...
HasMany(m => m.Foo).AsSet().Cascade.All();
HasMany(m => m.Bar).AsSet().Cascade.All();
}
public FooMap()
{
...
References(m => m.Parent);
}
public BarMap()
{
...
References(m => m.Parent);
}
Whenever I save the parent object I get
INSERT Parent ...
INSERT Foo with Parent_id set to NULL
INSERT Bar with Parent_id set to NULL
UPDATE Foo set Parent_id to Parent id
UPDATE Bar set Parent_id to Parent id
Is there a way apart from setting the relationship to Inverse
to avoid the extra update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有双向关联,您应该将 HasMany 端设置为反向,这是最常见的方法。
另一种可能性是将父引用设置为
Not.Insert().Not.Update()
,然后插入/更新时将完全忽略父属性。如果您不需要从子级到父级的引用,只需将其从类和映射中删除,这样您就只有集合了。
对于第二种和第三种方式,您可以在 HasMany 上另外设置
Not.KeyNullable()
,这可以确保 NHibernate 插入已设置父 ID 的新行,并避免额外的更新语句(此功能需要 NHibernate 3.2.0)。If you have a bidirectional assiciation you should set the HasMany side to inverse, this is the most common way.
Another possibility is setting the parent reference to
Not.Insert().Not.Update()
, then the parent property will be completely ignored for insert/updates.If you don't need the reference from child to parent just remove it from the class and mapping so you only have the collection.
For the second and third way you can additionally set
Not.KeyNullable()
on the HasMany, this makes sure that NHibernate inserts the new row with the parent ID already set and avoids the additional update statement (this feature requires NHibernate 3.2.0).据我所知,不是。
通过设置反向属性,您可以指定关联的所有者。
为什么指定逆属性对您来说是个问题?
AFAIK, not.
By setting the inverse property, you specify the owner of the association.
Why is it a problem for you to specify the inverse property ?