Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Castle ActiveRecord - Object Composed by itself
    text
    copied!<p>I'm trying to use the code bellow to represent a product composition, I can sucessfully insert a product, with a product list within it, but when I try to edit and save the "master product" again, it throws the following message to me: "a different object with the same identifier value was already associated with the session: 1, of entity: Project.Product"</p> <p>I believe that it has something to do with the product being persisted itself within each component, under MasterProduct property.</p> <p>Sorry about my english, I'm working on it. </p> <pre><code>Imports Castle.ActiveRecord &lt;ActiveRecord()&gt; _ Public Class Product Private _components As IList = New ArrayList Private _masterProduct As Product &lt;[HasAndBelongsToMany](GetType(Product), Table:="Components", _ ColumnKey:="productId", ColumnRef:="componentId", _ cascade:=ManyRelationCascadeEnum.AllDeleteOrphan)&gt; _ Public Overridable Property Components() As IList Get Return Me._components End Get Set(ByVal value As IList) Me._components = value End Set End Property &lt;BelongsTo()&gt; 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 </code></pre> <p>As requested, the persistance code, I've done some abstraction, so sorry if I've missed something..</p> <pre><code>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" &amp; Mid(Me.GetType.Name, 3))) Using SS As New SessionScope(FlushAction.Never) If Obj.Versao &lt;&gt; 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload