使用复合键进行 NHibernate 更新
我有一个表定义,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须使用composite-id
http://nhibernate。信息/doc/nh/en/index.html#mapping-declaration-compositeid
You have to use a composite-id
http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-compositeid
如果您的主键是复合的,您的映射应该反映这一点,并且您的类需要重写
Equals
和GetHashCode
。此外,如果
ClientId
是Client
实体的主键,则应将其映射为多对多
,而不仅仅是 Id。另外,为什么要指定
lazy="false"
?您知道其中的含义吗?另外,为什么要使用
access="field"
映射所有内容?这些属性是否有一些特殊的逻辑?这是考虑到我刚刚写的所有内容的修订后的映射。请随意忽略那些不适用的部分:-)
If you primary key is composite, your mapping should reflect that, and your class needs to override
Equals
andGetHashCode
.Also, if
ClientId
is the primary key of yourClient
entity, you should map it asmany-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 :-)
正如上面其他同志提到的,你必须使用composite-id,这不是最好的但可以接受的做法。
另一方面,您可以简单地编写一个 更新拦截器 并确保其中的 Type = 1。
以下是一些有关该主题的链接,可帮助您清楚地了解这一点。
使用拦截器而不是复合键的主要优点是它不会破坏您的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.
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.