[英]key-many-to-one and key-property association: nhibernate won't DELETE items from set
I'll try to keep this terse, but hopefully won't miss any important information in my troubles. 我会尽量保持这种简洁,但希望不要错过任何重要信息。 The code I believe provides all details, but I've left out the noise (it's VB, so there's lots of noise :) ).
我相信的代码提供了所有细节,但我遗漏了噪音(这是VB,所以有很多噪音:))。
A "Case" object has many "Assignments": “Case”对象有许多“赋值”:
Public Class Case
Property CaseId As Guid
Property Assignments As ISet(Of CaseAssignment)
End Class
Public Class CaseAssignment
Property Case As Case
Property RoleId As Guid
End Class
The data model I've been handed looks about like what you'd expect, except CaseAssignment is a composite key: 我已经交过的数据模型看起来与你期望的一样,除了CaseAssignment是一个复合键:
table Case
CaseId uniqueid not null primary key
...
table CaseAssignment
CaseId uniqueid not null
RoleId uniqueid not null
PK := (CaseId, RoleId)
FK from CaseId -> Case.CaseId
Finally, the Fluent NHibernate Mappings: 最后,Fluent NHibernate映射:
Class CaseMapping
Public Sub New()
Table("Case")
KeyColumn("CaseId")
HasMany(Function(x) x.Assignments).KeyColumn("CaseId").Cascade.AllDeleteOrphan()
End Class
Class CaseAssignmentMapping
Public Sub New()
Table("CaseAssignment")
CompositeId() _
.KeyReference(Function(x) x.Case, "CaseId") _
.KeyProperty(Function(x) x.RoleId)
End Class
KeyReference correlates with "key-many-to-one" in the XML mapping lingo. KeyReference与XML映射术语中的“key-many-to-one”相关联。
When I add assignments to a case all is good, but when I remove references I get one of two problems. 当我为一个案例添加任务时,一切都很好,但是当我删除引用时,我会遇到两个问题之一。 With this code:
使用此代码:
aCase.Assignments.Remove(someAssignment)
caseRepository.Save(aCase)
The error I get back is, "Could not delete collection rows... Cannot insert the value NULL into column 'CaseId', table 'CaseAssignments'; column does not allow nulls. UPDATE fails. The statement has been terminated." 我得到的错误是,“无法删除集合行...无法将值NULL插入列'CaseId',表'CaseAssignments';列不允许空值.UPDATE失败。语句已被终止。” This was from trying to issue the following SQL:
这是因为尝试发出以下SQL:
UPDATE CaseAssignments SET CaseId = null
WHERE CaseId = @p0 AND RoleId = @p1 AND CaseId = @p2
@p0=[valid guid #1],
@p1=[valid guid #2],
@p2=[valid guid #1 again] **!?!**
So that's a little messed up. 所以这有点搞砸了。 So I try this code:
所以我试试这段代码:
aCase.Assignments.Remove(someAssignment)
someAssignment.Case = Nothing
caseRepository.Save(aCase)
and the error is "Unexpected row count: 0; expected: 1" because NHibernate tried to: DELETE FROM CaseAssignments WHERE RoleId = [valid guid] AND CaseId = NULL 并且错误是“意外的行数:0;预期:1”因为NHibernate试图:DELETE FROM CaseAssignments WHERE RoleId = [valid guid] AND CaseId = NULL
I've been scouring threads and forums and the NHibernate and Hibernate docs and haven't really come across anything similar yet. 我一直在搜索线程和论坛以及NHibernate和Hibernate文档,并没有真正遇到类似的东西。 Hopefully it's something simple.
希望这很简单。 Thanks to anyone who takes a shot at this one!
感谢任何对此拍摄的人!
I had the same problem a few days ago. 几天前我遇到了同样的问题。 The solution is to put the "inverse" attribute to "true" on your collection of CaseAssignments in your CaseMapping class.
解决方案是在CaseMapping类的CaseAssignments集合中将“inverse”属性设置为“true”。 Like this:
像这样:
HasMany(Function(x) x.Assignments).KeyColumn("CaseId").Cascade.AllDeleteOrphan().Inverse()
As far as I know, you must have both the AllDeleteOrphan cascade type AND the Inverse property set to true for this to work. 据我所知,你必须同时将AllDeleteOrphan级联类型和Inverse属性设置为true才能生效。
I hope it works! 我希望它有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.