简体   繁体   English

NHibernate Definitive Cascade应用指南

[英]NHibernate Definitive Cascade application guide

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. 是否有任何互联网资源对NHibernate的所有级联设置有明确的指导,其中包括类结构,HBM的示例以及与NH的所有关系的每个级联设置的动作的含义。

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. 如果有以最正确的方式进行公共关联的示例,例如设置状态表,您将永远不会最终级联删除状态,或者删除具有CreatedBy用户属性的对象,那么它将会很有用。永远不会在级联等中删除用户

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). 以下内容改编自NHiberate 3.0的Java Hibernate参考http://docs.jboss.org/hibernate/stable/core/manual/en-US/html/objectstate.html#objectstate-transitive (即当前的svn trunk) 。

For each basic operation of the NHibernate Session - including Persist(), Merge(), SaveOrUpdate(), Delete(), Lock(), Refresh(), Evict(), Replicate() - there is a corresponding cascade style. 对于NHibernate会话的每个基本操作 - 包括Persist(),Merge(),SaveOrUpdate(),Delete(),Lock(),Refresh(),Evict(),Replicate() - 都有相应的级联风格。 Respectively, the cascade styles are named persist, merge, save-update, delete, lock, refresh, evict, replicate. 级联样式分别命名为persist,merge,save-update,delete,lock,refresh,evict,replicate。 The cascade style for Save() and Update() is save-update; Save()和Update()的级联样式是save-update; for SaveAndUpdateCopy() it is merge; 对于SaveAndUpdateCopy(),它是合并; and for PersistOnFlush() it is persist. 对于PersistOnFlush(),它是持久的。 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"/>

Cascade styles my be combined: 级联风格我的组合:

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

You can use cascade="all" to specify that all operations should be cascaded along the association. 您可以使用cascade =“all”指定所有操作都应该沿关联级联。 The default cascade="none" specifies that no operations are to be cascaded. 默认级联=“none”指定不进行级联操作。

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. 特殊的级联样式delete-orphan仅适用于一对多关联,并指示Delete()操作应应用于从关联中删除的任何子对象。 And all-delete-orphan is the same as all,delete-orphan. all-delete-orphan与all,delete-orphan相同。

Recommendations: 建议:

  • It does not usually make sense to enable cascade on a <many-to-one> or <many-to-many> association. 在<many-to-one>或<many-to-many>关联上启用级联通常没有意义。 Cascade is often useful for <one-to-one> and <one-to-many> associations. Cascade通常用于<one-to-one>和<one-to-many>关联。
  • If the child object's lifespan is bounded by the lifespan of the parent object, make it a life cycle object by specifying cascade="all-delete-orphan". 如果子对象的生命周期受父对象的生命周期限制,请通过指定cascade =“all-delete-orphan”将其设置为生命周期对象。
  • Otherwise, you might not need cascade at all. 否则,您可能根本不需要级联。 But if you think that you will often be working with the parent and children together in the same transaction, and you want to save yourself some typing, consider using cascade="persist,merge,save-update". 但是如果你认为你经常在同一个交易中与父母和孩子一起工作,并且你想节省一些打字,可以考虑使用cascade =“persist,merge,save-update”。

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. 使用cascade =“all”映射关联(单值关联或集合)将关联标记为父/子样式关系,其中父项的保存/更新/删除导致保存/更新/删除子项或儿童。 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". 除非在使用cascade =“delete-orphan”映射的<one-to-many>关联的情况下,否则不会自动删除未被其父级引用的子级。 The precise semantics of cascading operations for a parent/child relationship are as follows: 父/子关系的级联操作的精确语义如下:

  • If a parent is passed to Persist(), all children are passed to Persist() 如果将父级传递给Persist(),则所有子级都将传递给Persist()
  • If a parent is passed to Merge(), all children are passed to Merge() 如果将父项传递给Merge(),则所有子项都将传递给Merge()
  • If a parent is passed to Save(), Update() or SaveOrUpdate(), all children are passed to SaveOrUpdate() 如果将父级传递给Save(),Update()或SaveOrUpdate(),则所有子级都将传递给SaveOrUpdate()
  • If a transient or detached child becomes referenced by a persistent parent, it is passed to SaveOrUpdate() 如果一个临时或已分离的子进程被持久父进程引用,则将其传递给SaveOrUpdate()
  • If a parent is deleted, all children are passed to Delete() 如果删除父项,则将所有子项传递给Delete()
  • If a child is dereferenced by a persistent parent, nothing special happens - the application should explicitly delete the child if necessary - unless cascade="delete-orphan", in which case the "orphaned" child is deleted. 如果孩子被持久父母取消引用,则不会发生任何特殊情况 - 应用程序应在必要时明确删除子项 - 除非cascade =“delete-orphan”,在这种情况下,“孤立”子项将被删除。

This might be obvious advice but I would suggest browsing old post made by Ayende . 这可能是明显的建议,但我建议浏览Ayende制作的旧帖子。 A quick search for NHibernate and cascade on his site revealed a few interesting posts. 在他的网站上快速搜索 NHibernate和级联显示了一些有趣的帖子。 They might, however, be a bit too scarce for your needs. 但是,它们可能对您的需求有点过于稀缺。

Even though it is not a internet resource per se, I would also recommend NHibernate in Action . 即使它本身不是互联网资源,我也会推荐NHibernate in Action It addresses cascades in some depth in chapter 3, 4 and 6. The book targets NHibernate 1.2. 它在第3,4和6章深入讨论了级联。本书针对NHibernate 1.2。 I do believe, though, that there will be a new edition of the book targeting the 3.0 release of NHibernate; 不过,我相信会有一本针对NHibernate 3.0版本的新版本。 it might be worth keeping an eye on. 可能值得关注。

As much as I would have liked to see a definitive guide to cascades I have not seen one. 尽管我希望看到一个关于级联的权威指南,但我还没有看到过。 Maybe you could summarize some of the blog posts out there discussing cascades with your own post on your own blog. 也许你可以总结一些博客文章,在你自己的博客上用你自己的帖子讨论级联。

I don't know any "definitive" guide, but the best resource I know is a blog post from Ayende, who is one of the definitive gurus in NHibernate: 我不知道任何“权威”指南,但我知道的最好的资源是来自Ayende的博客文章,他是NHibernate的权威大师之一:

NHibernate Cascades: the different between all, all-delete-orphans and save-update NHibernate Cascades:所有,all-delete-orphans和save-update之间的区别

For me, I actually only use cascade="none" and cascade="all" . 对我来说,我实际上只使用cascade="none"cascade="all" all-delete-orphan is sometimes an option. all-delete-orphan有时是一个选项。 Everything else is suspicious. 其他一切都是可疑的。 For instance, why should I implicitly create an instance because it is referenced, when it lives longer then the containing object? 例如,为什么我应该隐式创建一个实例,因为它被引用,当它比包含对象长寿命时? For me, there are only two situations: either the object is a dependent or independent. 对我来说,只有两种情况:对象是依赖对象还是独立对象。

Accepted answer explains this in details with HBM files. 接受的答案详细解释了HBM文件。 This answer covers the same with Mapping By Code. 这个答案涵盖了按代码映射的相同内容。 They are almost same; 它们几乎相同; just mapped to their HBM strings. 刚刚映射到他们的HBM字符串。

Article form Ayende explains it well: 文章形式Ayende解释得很好:

  • none - do not do any cascades, let the users handles them by themselves. none - 不做任何级联,让用户自己处理它们。
  • save-update - when the object is saved/updated, check the associations and save/update any object that require it (including save/update the associations in many-to-many scenario). save-update - 保存/更新对象时,检查关联并保存/更新任何需要它的对象(包括在多对多方案中保存/更新关联)。
  • delete - when the object is deleted, delete all the objects in the association. delete - 删除对象时,删除关联中的所有对象。
  • delete-orphan - when the object is deleted, delete all the objects in the association. delete-orphan - 删除对象时,删除关联中的所有对象。 In addition to that, when an object is removed from the association and not associated with another object (orphaned), also delete it. 除此之外,当从关联中删除对象而不与另一个对象(孤立对象)关联时,也删除它。
  • all - when an object is save/update/delete, check the associations and save/update/delete all the objects found. all - 当对象保存/更新/删除时,检查关联并保存/更新/删除找到的所有对象。
  • all-delete-orphan - when an object is save/update/delete, check the associations and save/update/delete all the objects found. all-delete-orphan - 当一个对象被保存/更新/删除时,检查关联并保存/更新/删除找到的所有对象。 In additional to that, when an object is removed from the association and not associated with another object (orphaned), also delete it. 除此之外,当从关联中删除对象而不与另一个对象(孤立对象)关联时,也删除它。

Also, this question explains few inner implementations of Cascade . 此外, 这个问题解释了Cascade一些内部实现。

 [Flags] public enum Cascade { None = 0, Persist = 2, Refresh = 4, Merge = 8, Remove = 16, Detach = 32, ReAttach = 64, DeleteOrphans = 128, All = 256, } private static IEnumerable<string> CascadeDefinitions(this Cascade source) { if (source.Has(Cascade.All)) { yield return "all"; } if (source.Has(Cascade.Persist)) { yield return "save-update, persist"; } if (source.Has(Cascade.Refresh)) { yield return "refresh"; } if (source.Has(Cascade.Merge)) { yield return "merge"; } if (source.Has(Cascade.Remove)) { yield return "delete"; } if (source.Has(Cascade.Detach)) { yield return "evict"; } if (source.Has(Cascade.ReAttach)) { yield return "lock"; } if (source.Has(Cascade.DeleteOrphans)) { yield return "delete-orphan"; } } 

Please note that Cascade.All does not include Cascade.DeleteOrphans . 请注意, Cascade.All不包括Cascade.DeleteOrphans In that case, you may need to include it using Cascade.All | Cascade.DeleteOrphans 在这种情况下,您可能需要使用Cascade.All | Cascade.DeleteOrphans来包含它 Cascade.All | Cascade.DeleteOrphans . Cascade.All | Cascade.DeleteOrphans

Alternatively, you can use Include extension method Cascade.All.Include(Cascade.DeleteOrphans) . 或者,您可以使用Include扩展方法Cascade.All.Include(Cascade.DeleteOrphans)

In combination with Cascade , you may need to look into Inverse as well; Cascade结合使用时,您可能还需要查看Inverse ; it specified the owner of the association. 它指定了协会的所有者。 Refer to this question for more details. 有关详细信息,请参阅问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM