使用复合键进行 NHibernate 更新

发布于 2024-09-01 03:01:52 字数 900 浏览 1 评论 0原文

我有一个表定义,如下所示:

License

ClientId
Type
Total
Used

ClientId 和 Type 一起唯一标识一行。我有一个映射文件,如下所示:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="Acumen.AAM.Domain.Model.License, Acumen.AAM.Domain" lazy="false" table="License">
<id name="ClientId" access="field" column="ClientID" />
<property name="Total" access="field" column="Total"/>
<property name="Used" access="field" column="Used"/>
<property name="Type" access="field" column="Type"/>
  </class>
</hibernate-mapping>

如果客户端使用许可证来创建用户,我需要更新表中的“已使用”列。当我在映射 xml 中将 ClientId 列设置为该表的 id 列时,我收到 TooManyRowsAffectedException。

您能否让我知道如何在映射级别设置复合键,以便 NHibernate 可以根据 ClientId 和 Type 进行更新。

类似于:更新许可证集已使用=已使用-1 WHERE ClientId='xxx' AND Type=1

请帮忙。

谢谢, 马赫什

I have a table defnition as given below:

License

ClientId
Type
Total
Used

ClientId and Type together uniquely identifies a row. I have a mapping file as given below:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="Acumen.AAM.Domain.Model.License, Acumen.AAM.Domain" lazy="false" table="License">
<id name="ClientId" access="field" column="ClientID" />
<property name="Total" access="field" column="Total"/>
<property name="Used" access="field" column="Used"/>
<property name="Type" access="field" column="Type"/>
  </class>
</hibernate-mapping>

If a client used a license to create a user, I need to update the Used column in the table. As I set ClientId column as the id column for this table in the mapping xml, I am getting TooManyRowsAffectedException.

could you please let me know how to set a composite key at mapping level so that NHibernate can udpate based on ClientId and Type.

Something like: Update License SET Used=Used-1 WHERE ClientId='xxx' AND Type=1

Please help.

Thanks,
Mahesh

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

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

发布评论

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

评论(3

七颜 2024-09-08 03:01:52

如果您的主键是复合的,您的映射应该反映这一点,并且您的类需要重写 EqualsGetHashCode

此外,如果 ClientIdClient 实体的主键,则应将其映射为多对多,而不仅仅是 Id。

另外,为什么要指定lazy="false"?您知道其中的含义吗?

另外,为什么要使用 access="field" 映射所有内容?这些属性是否有一些特殊的逻辑?

这是考虑到我刚刚写的所有内容的修订后的映射。请随意忽略那些不适用的部分:-)

<class name="Acumen.AAM.Domain.Model.License, Acumen.AAM.Domain" table="License">
  <composite-id>
    <key-many-to-one name="Client" column="ClientID" />
    <key-property name="Type" />
  </composite-id>
  <property name="Total" />
  <property name="Used" />
</class>

If you primary key is composite, your mapping should reflect that, and your class needs to override Equals and GetHashCode.

Also, if ClientId is the primary key of your Client entity, you should map it as many-to-many, not just an Id.

Also, why are you specifying lazy="false"? Are you aware of the implications?

Also, why map everything with access="field"? Do the properties have some special logic?

This is a revised mapping considering everything I just wrote. Feel free to ignore those parts that don't apply :-)

<class name="Acumen.AAM.Domain.Model.License, Acumen.AAM.Domain" table="License">
  <composite-id>
    <key-many-to-one name="Client" column="ClientID" />
    <key-property name="Type" />
  </composite-id>
  <property name="Total" />
  <property name="Used" />
</class>
清醇 2024-09-08 03:01:52

正如上面其他同志提到的,你必须使用composite-id,这不是最好的但可以接受的做法。

另一方面,您可以简单地编写一个 更新拦截器 并确保其中的 Type = 1。

以下是一些有关该主题的链接,可帮助您清楚地了解这一点。

  1. 优雅的代码:实现 NHibernate 拦截器
  2. NHibernate 文档:拦截器
  3. NHibernate IInterceptor 实现示例
  4. 企业 .NET 社区:NHibernate 第 2 部分(向下滚动至:拦截器和持久生命周期
  5. NHibernate 拦截器审核插入的对象 ID (SO 问题)

使用拦截器而不是复合键的主要优点是它不会破坏您的DBRM 提供了一个绝对更灵活的解决方案,不会“污染”您的映射文件,这将更准确地表示您的模型。

As the other comrades mentioned above, you have to use a composite-id, which is not a best but acceptable practice.

On the other hand, you can simply write an update interceptor and make sure your Type = 1 within it.

Here are some link about the topic to help you see clear in this.

  1. Elegant code : Implementing NHibernate Interceptors
  2. NHibernate Documentation : Interceptors
  3. Sample NHibernate IInterceptor implementation
  4. Enterprise .NET Community : NHibernate Part 2 (Scroll down to : Interceptors and Persistent Lifecycle)
  5. NHibernate Interceptor Auditing Inserted Object Id (SO question)

The main advantage of using interceptors over a composite key is that it doesn't break your DBRM and provides a definitely more flexible solution, without "polluting" your mapping file which will more precisely represent your model.

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