Menu

I Cann't Save OneToMany Associated

2004-08-31
2004-08-31
  • Nobody/Anonymous

    current I use 0.1.8
    I try to use v.1.2.2
    but I can not save onetomany associated.
    It Only save Head But Detail not save.

    CPersistenceBroker.vb Why are you Remark So Many Lines ?

       Private Sub savePrivateObject(ByRef obj As CPersistentObject, ByVal conn As _CConnection)
            Dim j, i, k As Integer
            Dim clMap As CClassMap
            Dim m As Short
            Dim udaMap As CUDAMap
            Dim Value As CPersistentObject
            Dim col As CPersistentCollection
            Dim SharedClassMapStack As New Stack
            Dim cm As CClassMap
            Dim tmpStatement As CSqlStatement
            Dim tmpRs As CResultset
            Dim row As DataRow
            Dim tmpdate As Date
            Dim statement As CSqlStatement
            Dim rs As CResultset

            clMap = obj.getClassMap(obj)
            'classMapStack = New Stack
            cm = clMap
            'Clear dirty flag so that recursion in the class structure doesn't cause an infinite loop
            obj.IsDirty = False

            Debug.WriteLine("Saving object " & cm.Name)

            'For i = 1 To cm.getStraightAssociationMapSize
            '    udaMap = cm.getStraightAssociationMap(i)
            '    If udaMap.SaveAutomatic Then
            '        If udaMap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_ONE Then
            '            Value = obj.getObjectByAttribute(udaMap.Target)
            '            If Not Value Is Nothing Then
            '                'Use the objects save method (will check for dirtiness, vailidity, etc)
            '                Value.Save()
            '                'savePrivateObject(Value, conn)
            '                For j = 1 To udaMap.getSize
            '                    Value = obj.getObjectByAttribute(udaMap.Target)
            '                Next j
            '            End If
            '        ElseIf udaMap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_MANY Then
            '            col = obj.getCollectionByAttribute(udaMap.Target)
            '            If Not col Is Nothing Then
            '                For k = 0 To col.Count() - 1
            '                    Value = col.Item(k)
            '                    'Use the objects save method (will check for dirtiness, vailidity, etc)
            '                    Value.Save()
            '                    'savePrivateObject(Value, conn)
            '                Next k
            '            End If
            '        End If
            '    End If
            'Next i

            'Dim sharedClass As Boolean
            'sharedClass = True
            'Do
            '  If sharedClass Then SharedClassMapStack.Push(cm)
            '  If cm.SharedTableField Is Nothing Then
            '    sharedClass = False
            '  Else
            '    sharedClass = True
            '  End If
            '  cm = cm.SuperClass
            'Loop While Not cm Is Nothing
            'When saving objects with shared tables, save from the top most parent down
            'to handle inheritance.  Also, because of inheritance, when inserting new
            'objects, the first statement must be an INSERT but the rest should be
            'UPDATEs
            'Do While SharedClassMapStack.Count > 0
            '  cm = SharedClassMapStack.Pop
            If obj.Persistent Then
                statement = cm.getUpdateSqlFor(obj)
                conn.processStatement(statement)
            Else
                statement = cm.getInsertSqlFor(obj)
                conn.processStatement(statement)
                If cm.getIdentitySize > 0 Then
                    If CInt(cm.RelationalDatabase.getValueFor(obj.getValueByAttribute(cm.getIdentityAttributeMap(1).Name))) = 0 Then
                        obj.setAttributeValue(cm.getIdentityAttributeMap(1).Name, cm.RelationalDatabase.getIdentityValue(conn))
                    End If
                End If
                ''Set the object to persistent so that the next iteration uses an UPDATE
                'obj.Persistent = True
            End If
            'Loop

            'If obj.Persistent Then
            '  statement = cm.getUpdateSqlFor(obj)
            '  conn.processStatement(statement)
            '  'Save the superclass and its associations
            '  'If Not cm.SuperClass Is Nothing Then
            '  '    Value = obj.getObjectByClassMap(cm.SuperClass)
            '  '    Value.Save()
            '  '    'savePrivateObject(Value, conn)
            '  'End If
            'Else
            '  statement = cm.getInsertSqlFor(obj)
            '  conn.processStatement(statement)
            '  If cm.getIdentitySize > 0 Then
            '    If CInt(cm.RelationalDatabase.getValueFor(obj.getValueByAttribute(cm.getIdentityAttributeMap(1).Name))) = 0 Then
            '      obj.setAttributeValue(cm.getIdentityAttributeMap(1).Name, cm.RelationalDatabase.getIdentityValue(conn))
            '    End If
            '  End If
            '  'Save the superclass and its associations
            '  'If Not cm.SuperClass Is Nothing Then
            '  '    Value = obj.getObjectByClassMap(cm.SuperClass)
            '  '    Value.Save()
            '  '    'savePrivateObject(Value, conn)
            '  'End If
            'End If

            'For i = 1 To cm.getInverseAssociationMapSize
            '    udaMap = cm.getInverseAssociationMap(i)
            '    If udaMap.SaveAutomatic Then
            '        If udaMap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_ONE Then
            '            Value = obj.getObjectByAttribute(udaMap.Target)
            '            If Not Value Is Nothing Then
            '                'Use the objects save method (will check for dirtiness, vailidity, etc)
            '                Value.Save()
            '                'savePrivateObject(Value, conn)
            '                For j = 1 To udaMap.getSize
            '                    Value = obj.getObjectByAttribute(udaMap.getEntry(j).FromAttrMap.Name)
            '                Next j
            '            End If
            '        ElseIf udaMap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_MANY Then
            '            col = obj.getCollectionByAttribute(udaMap.Target)
            '            If Not col Is Nothing Then
            '                For k = 0 To col.Count() - 1
            '                    Value = col.Item(k)
            '                    'Use the objects save method (will check for dirtiness, vailidity, etc)
            '                    Value.Save()
            '                    'savePrivateObject(Value, conn)
            '                Next k
            '            End If
            '        End If
            '    End If
            'Next i
            'Need to reset the modified date to the new modified date after saving.
            'If we don't do this the object won't get saved correctly on subsequent calls.
            obj.ResetOriginalDates()
            obj.Persistent = True
            obj.IsDirty = False
            If m_useCache Then
                m_cache.Add(obj)     'Add to the cache
            End If
        End Sub

     
    • Richard Banks

      Richard Banks - 2004-08-31

      All the commented code is there from the previous version.  I try not to delete lines until I am sure I don't need them anymore.

      In terms of not being able to autosave the OneToMany association you should check that you have flagged the detail objects as dirty, and that you have set the association from Head to Detail as SaveAutomatic="true".

      If you have done this and it still doesn't work then please post a bug report with some details that I can look at.

      - Richard.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.