独特的约束 Nhibernate

发布于 2024-08-25 22:18:00 字数 176 浏览 2 评论 0原文

我有一个带有 Nhibernate 映射的对象,它有一个代理 ID 和一个自然 ID。由于自然 ID 受到唯一约束,如果对象已在数据库中具有相同的自然 ID,则插入查询将会失败。我的解决方案是在尝试插入之前手动检查自然 ID 是否在数据库中。

有没有办法指定 Nhibernate 在插入自然 ID/唯一约束之前进行选择?

I have a object with a Nhibernate mapping that has a surrogate ID and a natual ID. Since of cource the natural ID is uniquely constrained a insert query will fail if the object is already in the database with the same natural ID. My solution for this has been to manually check to see if natural IDs are in the database before trying to insert.

Is there a way to specify Nhibernate to do a select before insert on natural Id's/Unique Constraints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贩梦商人 2024-09-01 22:18:00

无论怎样,你都必须去数据库。

但是,您可以使用 NH Validator 以更透明的方式完成此操作。

阅读 Fabio Maulo 的以下帖子:http://fabiomaulo。 blogspot.com/2009/10/validation-through-persistence-not.html

In one way or another, you'll have to go to the DB.

However, you can do it in a more transparent way with NH Validator.

Read the following post from Fabio Maulo: http://fabiomaulo.blogspot.com/2009/10/validation-through-persistence-not.html

分分钟 2024-09-01 22:18:00

我最终为 Nhibernate 构建了一个 SaveOrUpdate 监听器,以便当对象即将保存到数据库时,我可以确定是否应该检查它们的唯一性。我不只是选择查看数据库中是否存在对象唯一属性,而是执行选择更新(悲观锁),以便该行将被锁定,以便我可以安全地合并和更新对象。确实会创建 O(2N) 查询,但如果它成为问题,我可以将其简化为单个合并语句。

http://en.wikipedia.org/wiki/Merge_(SQL)

I ended up building a SaveOrUpdate listener for Nhibernate so as objects are about to be saved to the database I can determine if their uniqueness should be checked. Instead of just selecting to see if the object unique properties exist in the database I do a select for update (pessimistic lock) so the row will be locked so I can safely merge and update the object. The does create O(2N) queries but if it becomes a problem I could simplify it into a single merge statement.

http://en.wikipedia.org/wiki/Merge_(SQL)

皓月长歌 2024-09-01 22:18:00

我通常通过尝试插入(提交更改)并捕获生成的异常来解决这种情况。

无论如何,你都必须访问数据库,因此失败的 INSERT 的成本基本上与 SELECT 相同(如果不是更便宜)……而且更安全。

I usually solve that kind of scenario by just trying to insert (submitting the changes) and catching the resulting exception.

You'll have to hit the DB anyway, so a failing INSERT has basically the same cost (if not cheaper) than a SELECT... And it's safer.

倾城泪 2024-09-01 22:18:00

我在保存或更新之前检查唯一性。然后我可以显示一条漂亮的验证消息。我发现很难读取异常并将约束违规与正确的字段相匹配。也许 NH 已经抽象了支持的数据库的错误代码......我还没有研究过。在极少数情况下,在检查和保存之间违反约束,用户会收到一般错误消息。

I check for uniqueness before save or update. Then I can display a nice validation message. I've found it difficult to read the exception and match the constraint violation to the correct field. Maybe NH has abstracted the error codes for supported databases...I haven't looked into that. In the rare case the constraint gets violated between the check and save, user gets a general error message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文