NHibernate 中的反向属性 [英] Inverse Attribute in NHibernate

查看:21
本文介绍了NHibernate 中的反向属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用反向属性?如果我没记错的话,对于一对多关系,逆属性必须设置为 true.对于多对多关系,实体类逆属性之一必须设置为 true,另一个设置为 false.

How do I use the Inverse Attribute? If I am not mistaken, for one to many relationships the inverse attribute must be set to true. For many-to-many relationships, one of the entity class inverse attributes must be set to true and another set to false.

任何人都可以对此有所了解?

Anyone can shed some lights on this?

推荐答案

inverse 属性不能设置为 true ...

The inverse attribute must not be set to true ...

您使用 inverse 属性来指定关联的所有者".(一个关联只能有一个所有者,因此一端必须设置为反向,另一端必须设置为非反向").(所有者:inverse=false;非所有者:inverse=true)

You use the inverse attribute to specify the 'owner' of the association. (An association can have only one owner, so one end has to be set to inverse, the other has to be set to 'non inverse'). (Owner: inverse=false; Non-owner: inverse=true)

在一对多的关联中,如果不将集合标记为反向端,那么NHibernate会额外执行一次UPDATE.实际上,在这种情况下,NHibernate 会先插入集合中包含的实体,必要时插入拥有集合的实体,然后更新'集合实体',从而设置外键和关联制作.(请注意,这也意味着您的数据库中的外键应该可以为空).

In a one-to-many association, if you do not mark the collection as the inverse end, then NHibernate will perform an additional UPDATE. In fact, in this case, NHibernate will first insert the entity that is contained in the collection, if necessary insert the entity that owns the collection, and afterwards, updates the 'collection entity', so that the foreign key is set and the association is made. (Note that this also means that the foreign key in your DB should be nullable).

当您将集合结束标记为inverse"时,NHibernate 将首先持久化拥有"该集合的实体,然后再持久化集合中的实体,避免额外的 UPDATE 语句.

When you mark the collection end as 'inverse', then NHibernate will first persist the entity that 'owns' the collection, and will persist the entities that are in the collection afterwards, avoiding an additional UPDATE statement.

所以,在双向关联中,你总是有一个反向端.

So, in an bi-directional association, you always have one inverse end.

这篇关于NHibernate 中的反向属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆