简体   繁体   English

Hibernate级联删除包

[英]Hibernate cascade delete on a bag

Having built most of my DAL using NHibernate, I've now discovered that the SQL Server's Cascade On Delete rules also need to be taken into account in my HBM files too. 使用NHibernate构建了大部分DAL后,我现在发现在我的HBM文件中也需要考虑SQL Server的Cascade On Delete规则。 I've been using bags for all my collections, but there doesn't seem to be a way to add a cascade="delete" attribute to a bag. 我一直在为我的所有收藏品使用包,但似乎没有办法将一个cascade =“delete”属性添加到包中。 I can change all my bags to sets, but that appears to mean I have changing all my IList<>s on my models to PersistentGenericSet<>s, which I don't really fancy doing. 我可以将所有行李更改为套装,但这似乎意味着我已将我模型上的所有IList <>更改为PersistentGenericSet <> s,我并不喜欢这样做。

Any advice? 有什么建议?

Anthony 安东尼

There are two separate-but-related concepts: NHibernate's cascading rules, and DB cascading. 有两个独立但相关的概念:NHibernate的级联规则和DB级联。

The latter is actually configured on the key element so, if the FK has ON DELETE CASCADE , this is what the bag should look like: 后者实际上是在key元素上配置的,所以,如果FK有ON DELETE CASCADE ,这就是包应该是这样的:

<bag name="Children" ...>
  <key column="ParentId" on-delete="cascade"/>
  <one-to-many class="Child"/>
</bag>

You can add a cascade attribute to a bag mapping. 您可以向行李贴图添加级联属性。 The documentation lists several options for the attribute cascade="all|none|save-update|delete|all-delete-orphan" . 文档列出了属性cascade="all|none|save-update|delete|all-delete-orphan"几个选项。 The most commonly used option for a one-to-many relationship is all-delete-orphan . 一对多关系最常用的选项是all-delete-orphan Setting cascade to this value will cause all database operations to be cascaded to the collection and child objects will be deleted if they are removed from the collection (orphaned). 将级联设置为此值将导致所有数据库操作级联到集合,并且如果从集合中删除子对象(孤立对象),则将删除子对象。

Database cascades are similar but do not offer the ability to automatically delete an orphaned child record. 数据库级联类似,但不提供自动删除孤立子记录的功能。 Setting the cascade option in NHibernate and in the database is somewhat redundant but may be useful if you have other systems accessing the database directly. 在NHibernate和数据库中设置级联选项有点多余,但如果您有其他系统直接访问数据库,则可能很有用。

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

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