Recursive Save not finding all linked objects

Posts   
 
    
tmarosti
User
Posts: 8
Joined: 03-May-2005
# Posted on: 03-May-2005 07:18:37   

We are using the LLBLGen product to translate a normal database structure into a generic set of tables. For example, a single field is represented in a table called an Act. One of the properties of the Act is the field name and another property is the value. All of the database tables and fields are stored in this manner, but some have different tables such as "Entity (not to be confused with a LLBLGen entity)", and "Role". Parent-child relationships are mapped using intermediate tables such as "ActRelationship". Thus, a normal child table of 10 fields will have 10 Act records and each will have an ActRelationship record to relate it back to the parent. The code I have constructed is as follows:


Dim ActParent as New ActEntity(Id)
Dim ActChild as New ActEntity
Dim ActRel1 As ActRelationshipEntity
Dim ActInt As ActEntity
Dim bHasActObs As Boolean
For Each ActRel1 In ActParent.ActRelationships_Source
    If ActRel1.TypeCode = "COMP" Then
        ActInt = ActRel1.Act_Target
        With ActInt
            If .ClassCode = A_UNIQUE_FIELD_NAME And .Code = "REF" Then
                bHasActObs = True
                Exit For
            End If
        End With
        ActInt = Nothing
    End If
    ActRel1 = Nothing
Next
If Not bHasActObs And SetIfNotFound Then
    ActInt = New ActEntity
    With ActInt
        .ClassCode = A_UNIQUE_FIELD_NAME
        .Code = "REF"
    End With
    ActRel1 = New ActRelationshipEntity
    With ActRel1
        .TypeCode = "COMP"
    End With
End If
If Not ActRel1 Is Nothing And Not ActInt Is Nothing Then
    With ActRel1
        .Act_Source = ActParent 
        .Act_Target = ActInt
    End With
End If
Dim ActRel2 As ActRelationshipEntity
Dim blnHasRelationship As Boolean
For Each ActRel2 In eAct.ActRelationships_Source
    If ActRel2.Target_ID = Me.ID And ActRel2.TypeCode = "COMP" Then
        blnHasRelationship = True
        Exit For
    End If
    ActRel = Nothing
Next
If Not blnHasRelationship Then
    ActRel2 = New ActRelationshipEntity
    ActRel2.TypeCode = "COMP"
End If
If Not ActRel2 Is Nothing Then
    ActRel2.Act_Source = eActInt
    ActRel2.Act_Target = Me
End If
ActParent.Save(True)

I am using VB.NET ASP and LLBLGEn in SelfServicing mode. On a typical web page I would store approximately 30-50 fields in this manner. When I save the ActParent some of the child fields do not get recognized and saved. Is there anyway I can break the process in debug mode and see where the linking is breaking down at?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 03-May-2005 10:41:02   

You should be able in the debugger to traverse the graph, though in VB.NET it's a bit tricky as you can't see private member variables in the locals window.

With 1.0.2004.2 you can switch on tracing, which will produce both the trace information for which entities get saved and when and also the SQL generated with which values. This way you can check if all the entities are indeed saved or not.

The code is a bit complex to understand without the deeper background of why the steps are taken as they are taken in the code, so I can't directly pinpoint where something goes wrong. for example what was the value of 'SetIfNotFound' when you tested it? And what's 'eAct' and 'eActInt' ?

Frans Bouma | Lead developer LLBLGen Pro
tmarosti
User
Posts: 8
Joined: 03-May-2005
# Posted on: 03-May-2005 19:18:57   

Thanks, I will try to use the tracing and get back to you.

Sorry about the code. It is complex and the snippet I posted was pieced together from a number of functions to make it more readable. The code in question is a common set of code used by many classes (by the way I amd sub-classing each of the generated entity classes) and I wanted to change the variable names to reflect the structure. "eAct" is "ActChild" and "eActInt" is "ActInt". The "SetIfNotFound" variable is true in this instance. I could post a more representative example but the listing would be quite long. Let me know if you need it.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 03-May-2005 20:25:11   

Ok. If you're subclassing the entities, be SURE you're not using IList typed collections for entity collections in the entities. It can be that that's the problem. Could you check that for me please?

Frans Bouma | Lead developer LLBLGen Pro
tmarosti
User
Posts: 8
Joined: 03-May-2005
# Posted on: 04-May-2005 19:41:13   

Thanks to your tracing function, I figured out my problem. The bottom line is that the logic I was using to determine the existence of an "actRelationship" object was faulty. The code loops through the collection and compares the ID fields to determine if the "actRelationship" object exists or not.

    Dim ActRel2 As ActRelationshipEntity
    Dim blnHasRelationship As Boolean
    For Each ActRel2 In eAct.ActRelationships_Source
        If **ActRel2.Target_ID = Me.ID** And ActRel2.TypeCode = "COMP" Then
            blnHasRelationship = True
            Exit For
        End If
        ActRel = Nothing
    Next

The problem is when you are creating many new objects and not saving them before you create the next one the ID properties of the objects return 0. Because I was calling the code over and over again for each new object, each loop iteration was finding the previous "actRelationship" object and replacing the previously created "act" object with the new one. Thanks, and keep working on those debugging tools in future products.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 05-May-2005 11:45:42   

aha! simple_smile Yeah, it has to set the value to 0, as it doesn't have a value yet. (identity fields).

I'm glad you like the tracing functionality. I hope to extend it to areas in the generated code if people want that / need that.

Frans Bouma | Lead developer LLBLGen Pro