控制 NHibernate 复合元素的唯一性

发布于 2024-10-04 22:32:38 字数 670 浏览 0 评论 0原文

我有以下映射:

...
<set name="Tests" table="InstrumentTests">
   <key column="InstrumentId" />
   <composite-element class="InstrumentTest">
      <property name="TestInstrumentId" not-null="true" />
      <property name="OtherTestId" />
   </composite-element>
</set>

我想让 InstrumentId 和 TestInstrumentId 唯一,因此表 InstrumentTest 不能包含:

InstrumentId TestInstrumentId OtherTestId
 1             TEST1            ABC
 1             TEST1            BCD  <--- NO
 2             TEST1            ABC  <--- OK, different InstrumentId

也许我想要的使用复合元素是不可能的,但是我应该如何映射它。

I have the following mapping:

...
<set name="Tests" table="InstrumentTests">
   <key column="InstrumentId" />
   <composite-element class="InstrumentTest">
      <property name="TestInstrumentId" not-null="true" />
      <property name="OtherTestId" />
   </composite-element>
</set>

I'd like to make InstrumentId and TestInstrumentId unique so the table InstrumentTest cannot contain:

InstrumentId TestInstrumentId OtherTestId
 1             TEST1            ABC
 1             TEST1            BCD  <--- NO
 2             TEST1            ABC  <--- OK, different InstrumentId

Maybe what I want is imposible using composite-element, but then how should I map this.

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

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

发布评论

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

评论(1

尾戒 2024-10-11 22:32:38

好的,我找到了解决方案。我需要为 InstrumentTest 类实现 Equals 和 GetHashCode。

nHiberntate 文档中明确指出:

注意:如果定义复合元素的 ISet,正确实现 Equals() 和 GetHashCode() 非常重要。

Ok, I found the solution. I need to implement Equals and GetHashCode for the InstrumentTest class.

It's clearly stated on the nHiberntate documentation:

Note: if you define an ISet of composite elements, it is very important to implement Equals() and GetHashCode() correctly.

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