views:

11

answers:

0

Hi,

I'm having problem with a loop which is supposed to add nHibernate objects to an existing nHibernate objects and save. There's a whole tree of relationships here there's a Page object which can have many Region objects which in turn can have many Row objects which can have many Asset objects - the problem is somewhere in the latter three and I've provided the mappings below:

AssetMap

Public Sub New()
    Id(Function(x) x.Id)
    Map(Function(x) x.DisplayOrder)
    Map(Function(x) x.Text).Length(10000)
    Map(Function(x) x.Title)
    Map(Function(x) x.Width)
    Map(Function(x) x.Height)
    References(Function(x) x.Image).LazyLoad()
    References(Function(x) x.Row).Cascade().All()
    Map(Function(x) x.AssetType).CustomType(Of AssetType)()
End Sub

RowMap

Public Sub New()
    Id(Function(x) x.Id)
    Map(Function(x) x.CssBackgroundId)
    Map(Function(x) x.DisplayOrder)
    Map(Function(x) x.RowType).CustomType(Of RowType)()
    HasMany(Function(x) x.Assets).OrderBy("DisplayOrder").Cascade.All()
    References(Function(x) x.Region).Cascade().All()
End Sub

RegionMap

Public Sub New()
    Id(Function(x) x.Id)
    References(Function(x) x.RegionTemplate)
    HasMany(Function(x) x.Rows).OrderBy("DisplayOrder").Cascade.All()
    Map(Function(x) x.RegionId)
    References(Function(x) x.Workflow)
    References(Function(x) x.Page)
    Map(Function(x) x.Ranking)
End Sub

The problematic code is supposed to loop through some data holding object, create a Row object then create some Asset objects, add them to the Row object and then add the Row to a collection. Finally the newly populated Row is added to the Region object.

The code works okay when you first start setting up Rows and Assets for a Region. The trouble starts when you have to makes changes to it and it gets saved a second time - at that point nHibernate creates two entirely new row objects, attaches the existing Assets to them, and orphans the two old rows by leaving them with null Id's.

   For Each oAsset As FrontEndService.PageAsset In assetCollection
        Dim oRow = New Cms.DataTransferObjects.Row
        oRow.Assets = BuildAssets(oAsset, oRow, regionPos, oPage)
        oRowCollection.Add(orow)
    Next

Private Function BuildAssets(ByVal oAsset As FrontEndService.PageAsset, ByVal oRow As Cms.DataTransferObjects.Row, ByVal RegionPos As Integer, ByRef oPage As Cms.DataTransferObjects.Page) As List(Of Cms.DataTransferObjects.Asset)
    Dim oColumnCollection As New List(Of Cms.DataTransferObjects.Asset)

    oRow.DisplayOrder = oAsset.pageAssetOrder
    oRow.RowType = DirectCast(CInt(oAsset.pageAssetType), Cms.DataTransferObjects.RowType)
    Dim noOfColumns As Integer = GetAmountOfCellsByType(oAsset.pageAssetType)

    For iColumncount As Int32 = 0 To noOfColumns - 1
        Dim oPageColumn = New Cms.DataTransferObjects.Asset
        oPageColumn.DisplayOrder = iColumncount + 1
        oPageColumn.AssetType = GetAssetType(oAsset.pageAssetType, iColumncount + 1)
        oPageColumn.Width = CInt(oPage.Regions(RegionPos).RegionTemplate.Width / noOfColumns)
        oColumnCollection.Add(oPageColumn)
    Next

    Return oColumnCollection
End Function

Can someone tell me what I'm doing wrong?