我正在尝试使用下面的代码来表示产品组成,我可以成功地插入产品,其中包含产品列表,但是当我尝试再次编辑并保存“主产品”时,它会抛出以下信息给我: “具有相同标识符值的不同对象已与会话关联:1,实体:Project.Product”
我认为这与在MasterProduct属性下每个组件中持久保存的产品有关。
对不起我的英语,我正在努力。
Imports Castle.ActiveRecord
<ActiveRecord()> _
Public Class Product
Private _components As IList = New ArrayList
Private _masterProduct As Product
<[HasAndBelongsToMany](GetType(Product), Table:="Components", _
ColumnKey:="productId", ColumnRef:="componentId", _
cascade:=ManyRelationCascadeEnum.AllDeleteOrphan)> _
Public Overridable Property Components() As IList
Get
Return Me._components
End Get
Set(ByVal value As IList)
Me._components = value
End Set
End Property
<BelongsTo()>
Public Overridable Property MasterProduct() As Product
Get
Return Me._masterProduct
End Get
Set(ByVal value As Product)
Me._masterProduct = value
End Set
End Property
End Class
根据要求,持久性代码,我做了一些抽象,很抱歉,如果我错过了什么......
Public Class Controller
Private _view As Object
Public Property View() As Object
Get
Return _view
End Get
Set(ByVal value As Object)
_view = value
End Set
End Property
'Here the controller opens a new view, and get the data for it..
'In this method all the Products will be listed in a treeview, so the users can select and edit them.
Public Overridable Sub openModule()
'Code(...)
Dim genericView As New frmGenericSelection(Title, Me)
With genericView
'Code(...)
Using SS As New SessionScope(FlushAction.Never)
call LoadAllProducts(.lstProducts)
End Using
Call .ShowDialog()
End With
End Sub
'This is the saving method, it is called by the "frmGenericSelection", passing the SelectedObject for edition.
'Or, if there's a new product, passing "New Product"..
Public Sub EditObject(ByVal Obj As Object)
If (View Is Nothing) Then View = Activator.CreateInstance(Type.GetType("Project.frm" & Mid(Me.GetType.Name, 3)))
Using SS As New SessionScope(FlushAction.Never)
If Obj.Versao <> 0 Then Obj.Refresh()
View.updateView(Obj)
If (View.ShowDialog() = DialogResult.OK) Then 'User pressed the "Save" button, the view has returned to the controller, telling it to save the object..
Try
Obj.SaveAndFlush()
MsgBox("Sucefully Saved!", MsgBoxStyle.Information, Obj.Title)
Catch ex As Exception
'Code(...)
End Try
End If
Obj = Nothing
View = Nothing
End Using
End Sub
End Class