问题描述
是否存在有一明确的指导所有的对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参考以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会话的每个基本操作 - 包括坚持(),合并(),saveOrUpdate()方法,删除()锁(),刷新(),集中退出(),复制() - 有相应的级联风格。分别级联样式命名持续,合并,保存,更新,删除,锁定,刷新,驱逐,复制。为保存()和更新()级联样式保存更新;对于SaveAndUpdateCopy()是合并;和PersistOnFlush()是坚持。并删除是删除别名。
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"/>
级联样式是可组合的:
Cascade styles my be combined:
<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.
一个特殊的级联风格,删除孤儿,只适用于一to-many关联,并表示删除()操作应该被应用于所有从关联中删除的对象。 。和全删除,孤儿是一样的一切,删除孤儿
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.
建议:
- 它没有通常意义上启用级联<许多一对一>或LT;许多一对多>协会。级联通常是与所述有用;一种对一>和<一对许多>关联。
- 如果子对象的寿命是由父对象的生命周期为界,使之成为生命周期对象通过指定cascade =全删除,孤儿。
- 否则,你可能根本不需要级联。但是,如果你认为你会经常与家长和孩子在同一事务一起工作,并希望自己节省一些,可以考虑采用级联=坚持,合并,保存更新。
映射与级联协会(一个单值关联或集合)=所有,标志着该协会作为父母/孩子风格的关系,保存/更新/删除保存/更新父结果/删除儿童或儿童。这成为其母公司未引用一个孩子不会自动删除,除了在的情况下,A<一种一对多>协会级联映射=删除孤儿。级联操作的父/子关系的准确语义如下:
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()
- 如果父母是传递到保存(),更新()或者saveOrUpdate(),所有的孩子则会被saveOrUpdate()
- 如果一个瞬时的或脱的孩子被一个持久化的父对象引用,它被传递给saveOrUpdate()方法
- 如果父对象被删除,所有的孩子都被delete()
- 如果一个孩子被持久化的父,无解引用特殊情况 - 必要时,程序应该明确的删除孩子 - 除非级联=删除孤儿,在这种情况下,成为孤儿的孩子被删除。
- If a parent is passed to Persist(), all children are passed to Persist()
- If a parent is passed to Merge(), all children are passed to Merge()
- If a parent is passed to Save(), Update() or SaveOrUpdate(), all children are passed to SaveOrUpdate()
- If a transient or detached child becomes referenced by a persistent parent, it is passed to SaveOrUpdate()
- If a parent is deleted, all children are passed to 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.
这篇关于NHibernate的权威级联应用指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!