NHibernate Definitive Cascade 应用指南 [英] NHibernate Definitive Cascade application guide

查看:16
本文介绍了NHibernate Definitive Cascade 应用指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何互联网资源对 NHibernate 的所有级联设置有明确的指南,其中将包括类结构示例、HBM 以及每个级联设置的操作对与 NH 的所有关系的影响.

Are there any internet resources that have a definitive guide to all of the cascade settings for NHibernate that will include examples of the class structure, HBM and the implications of actions with each of the cascade settings for all of the relationships with NH.

如果有以最正确的方式完成常见关联的示例,例如设置状态表,您将永远不会级联删除状态,或者删除具有 CreatedBy 的对象,那么这也会很有帮助用户属性永远不会以级联等方式删除用户.

Also it would be helpful if there were examples for common associations to be done in the most correct manner such as setting up a states table that you will never end up cascade deleting a state, or that deleting an object that has a CreatedBy User property will never end up deleting the User in a cascade etc.

推荐答案

以下内容改编自 Java Hibernate 参考 http://docs.jboss.org/hibernate/stable/core/manual/en-US/html/objectstate.html#objectstate-transitive 用于 NHiberate 3.0(即当前的 svn 主干).

The following is adapted from the Java Hibernate reference http://docs.jboss.org/hibernate/stable/core/manual/en-US/html/objectstate.html#objectstate-transitive for NHiberate 3.0 (ie the current svn trunk).

对于NHibernate Session的每一个基本操作——包括Persist()、Merge()、SaveOrUpdate()、Delete()、Lock()、Refresh()、Evict()、Replicate()——都有对应的级联风格.级联样式分别命名为persist、merge、save-update、delete、lock、refresh、evict、replicate.Save() 和 Update() 的级联样式是 save-update;对于 SaveAndUpdateCopy() 是合并;对于 PersistOnFlush() 它是持久的.而remove是delete的别名.

For each basic operation of the NHibernate Session - including Persist(), Merge(), SaveOrUpdate(), Delete(), Lock(), Refresh(), Evict(), Replicate() - there is a corresponding cascade style. Respectively, the cascade styles are named persist, merge, save-update, delete, lock, refresh, evict, replicate. The cascade style for Save() and Update() is save-update; for SaveAndUpdateCopy() it is merge; and for PersistOnFlush() it is persist. And remove is an alias for delete.

如果您希望操作沿关联级联,则必须在映射文档中指明.例如:

If you want an operation to be cascaded along an association, you must indicate that in the mapping document. For example:

<one-to-one name="person" cascade="persist"/>

层叠样式可以组合:

<one-to-one name="person" cascade="persist,delete,lock"/>

您可以使用cascade="all" 来指定所有操作都应该沿着关联进行级联.默认的 cascade="none" 指定不级联任何操作.

You can use cascade="all" to specify that all operations should be cascaded along the association. The default cascade="none" specifies that no operations are to be cascaded.

一种特殊的级联样式,delete-orphan,仅适用于一对多关联,并指示应该将 Delete() 操作应用于从关联中删除的任何子对象.all-delete-orphan 和 all,delete-orphan 一样.

A special cascade style, delete-orphan, applies only to one-to-many associations, and indicates that the Delete() operation should be applied to any child object that is removed from the association. And all-delete-orphan is the same as all,delete-orphan.

建议:

  • 在 <many-to-one> 上启用级联通常没有意义.或<多对多>协会.级联通常用于<一对一>和<一对多>关联.
  • 如果子对象的生命周期受父对象的生命周期限制,则通过指定级联="all-delete-orphan"使其成为生命周期对象.
  • 否则,您可能根本不需要级联.但是,如果您认为您将经常在同一个事务中与父级和子级一起工作,并且您想为自己节省一些输入,请考虑使用级联 =persist,merge,save-update".

使用cascade="all" 映射关联(单值关联或集合)将关联标记为父/子样式关系,其中父项的保存/更新/删除导致保存/更新/删除孩子或孩子.除非在 <一对多> 的情况下,否则不会被其父项未引用的子项自动删除.关联映射为级联 =删除孤儿".父/子关系级联操作的精确语义如下:

Mapping an association (either a single valued association, or a collection) with cascade="all" marks the association as a parent/child style relationship where save/update/delete of the parent results in save/update/delete of the child or children. A child which becomes unreferenced by its parent is not automatically deleted, except in the case of a <one-to-many> association mapped with cascade="delete-orphan". The precise semantics of cascading operations for a parent/child relationship are as follows:

  • 如果父级传递给 Persist(),则所有子级都传递给 Persist()
  • 如果父级传递给 Merge(),则所有子级都传递给 Merge()
  • 如果父级传递给 Save()、Update() 或 SaveOrUpdate(),则所有子级都传递给 SaveOrUpdate()
  • 如果一个临时或分离的子节点被持久父节点引用,它会被传递给 SaveOrUpdate()
  • 如果删除了父级,则所有子级都将传递给 Delete()
  • 如果一个孩子被一个持久的父级取消引用,没有什么特别的事情发生 - 应用程序应该在必要时明确删除这个孩子 - 除非级联 =delete-orphan",在这种情况下,孤立"的孩子被删除.

这篇关于NHibernate Definitive Cascade 应用指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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