如何实现数据约束以强制执行最小数量的子对象?
假设每个 AssetGroup 对象必须至少有 1 个 Asset 对象。如何在以下位置强制执行此约束: a) 传统的SQL b) NHibernate
我可以在删除操作之前检查子资源的数量,但也许有更多声明性的方法来做到这一点。
如果创建 AssetGroup,是否会强制我在创建 AssetGroup 之前先创建 Asset?
Suppose each AssetGroup object must have at least 1 Asset object . How to enforce this constraint in :
a) traditional SQL
b) NHibernate
I can check the number of children Asset before delete operation , but perhaps there are more declarative ways to do it .
In case of creating AssetGroup, does it force me to create an Asset first before creating an AssetGroup ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你试图在错误的层面上处理这个问题。
这是业务级别的约束,而不是数据级别的约束。在您的业务层进行验证就可以了。
我所说的“业务层”是指实体本身,或者数据(NH/存储库)层之上的任何内容。
You're trying to deal with that at the wrong level.
This is a business-level constraint, not a data-level one. Do the validation in your business layer and you'll be fine.
By "business layer" I mean either the entities themselves, or whatever you have on top of the data (NH/Repository) layer.
三种可能的方法:
a) 手动“更新集memberCount = memberCount + 1”
b) 通过触发器执行此操作(看起来像 1.,但具有存储的状态)
Three possible ways:
a) manual 'update set memberCount = memberCount + 1'
b) do this via trigger (looks like 1., but with stored state)