问题描述
我会尽量保持这个简洁,但希望不会错过任何重要的信息在我的麻烦。我相信的代码提供了所有的细节,但是我省去了噪音(这是VB,所以有很多噪音:))。Case对象有许多分配:
pre code $公共类案例
属性CaseId作为Guid
属性Assignments As ISet(Of CaseAssignment)
结束类
公共类CaseAssignment
属性案例为例
属性RoleId为Guid
结束类
$ c $数据模型我已经交给看起来像你所期望的,除了CaseAssignment是一个组合键:
$ b
表格Case
CaseId uniqueid不为null主键
...
$ b $表格CaseAssignment
CaseId uniqueid not null
RoleId uniqueid not null
PK:=(CaseId,RoleId)
来自CaseId的FK - > Case.CaseId
最后,Fluent NHibernate映射:
code $ Class $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $函数(x)x.Assignments).KeyColumn(CaseId)。Cascade.AllDeleteOrphan()
结束类
类CaseAssignmentMapping
Public Sub New()
Table(CaseAssignment)
CompositeId()_
.KeyReference(Function(x)x.Case,CaseId)_
.KeyProperty(Function(x)x.RoleId)
End Class
KeyReference与XML映射中的key-many-to-one关联lingo。
当我为案例添加任务都很好,但是当我删除引用时,我得到两个问题之一。有了这个代码:
pre $ $ $ $ $ $ $ $ $ $
我得到的错误是无法删除集合行...无法将值NULL插入列'CaseId ',表'CaseAssignments';列不允许为NULL,UPDATE失败
语句已被终止。这是从尝试发出以下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 ] **!?!**
所以这有点搞砸了。所以我试试这个代码:
$ $ $ $ $ $ $ $ b caseRepository.Save(aCase)
,错误是意外的行数:0; expected:1 因为NHibernate尝试:
DELETE FROM CaseAssignments WHERE RoleId = [有效guid] AND CaseId = NULL
淘金线程和论坛,NHibernate和Hibernate文档,还没有真正遇到过类似的东西。希望这是简单的。感谢任何一个在这一个拍摄的人!
我前几天也有同样的问题。解决的办法是在您的CaseMapping类的CaseAssignments集合上设置inverse属性为true。像这样:
HasMany(Function(x)x.Assignments).KeyColumn(CaseId)。Cascade.AllDeleteOrphan .Inverse()
就我所知,您必须同时使用AllDeleteOrphan级联类型和Inverse属性设置为true这个工作。
我希望它的工作!
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 :) ).
A "Case" object has many "Assignments":
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:
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:
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.
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." This was from trying to issue the following 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
I've been scouring threads and forums and the NHibernate and Hibernate docs and haven't really come across anything similar yet. 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. 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.
I hope it works!
这篇关于key-many-to-one和key-property association:nhibernate不会从set中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!