Mismatched entities fetched on self-referencing table

Posts   
 
    
Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 10-Mar-2009 14:41:15   

I'm using the latest version (mar 2009) of LLBLGen Pro 2.6, with VS 2008 Sp1 / .Net 2.0. The DB is access.

I'm having an issue related to fetching an entity collection with related data. I've built a little project reproducing the issue.

There is a single table (MyEvent); in this table there is a list of "events". The columns are: IDEvent (key), Canceled (Boolean), IDPreviousEvent (unique, not required), IDPreviousEventForCanceledEvent(NOT unique, not required), Description

Each event can refer a single PreviousEvent. (IDPreviousEvent is unique) Each event can be "canceled". Each event can be referenced by multiple canceled events. (IDPreviousEventForCanceledEvent is NOT unique)

There are 2 relations:

MyEvent.IDPreviousEvent 1 ---> 1 MyEvent.IdEvent MyEvent.IDPreviousEventForCanceledEvent m ---> 1 MyEvent.IdEvent

When I load events without a prefetch path, everything go fine (see the button "Test 1" in the attached project) And the data looks like

IDEvent, Canceled,  IDPreviousEvent, IDPreviousEventForCanceledEvent, Description
      1  False                                                   Event A
      2  False               1                                 Event B
      3  False               2                                 Event C
      4  True                                                  3  Event D1 canceled
      5  True                                                  3  Event D2 canceled
      6  True                                                  3  Event D3 canceled
      7  False               3                                 Event D4 ok
      8  False               7                                 Event E
      9  False               8                                 Event F

But when I load events with a prefetch path for loading related "Previous Event", the data and the "PreviousEventEntity" fetched get mismatched/messed-up, and the data looks like:

IDEvent, Canceled,  IDPreviousEvent, IDPreviousEventForCanceledEvent, Description
      1  False               2                                 Event A
      2  False               3                                 Event B
      3  False               7                                 Event C
      4  True                                                  3  Event D1 canceled
      5  True                                                  3  Event D2 canceled
      6  True                                                  3  Event D3 canceled
      7  False               8                                 Event D4 ok
      8  False               9                                 Event E
      9  False               8                                 Event F

See the changed data in "IDPreviousEvent" column, these aren't the values in the DB. They get mismatched/messed-up due to prefetch path trying to load the "PreviousEntity".

I got stuck on this, and I didn't test if the others property/prefetch path (NextEvent / NextCanceledEvents / PreviosuEventForCanceledEvent) works correctly.

Another issue I've found is that the designer is a little unclear about the descriptions of relations in self-referencing table. For the "PreviousEvent" field_on_relation it say:

Returns one instance of the entity 'MyEvent' which are directly related to the instance of the entity 'MyEvent' where MyEvent.IdpreviousEvent=MyEvent.Idevent

I believe that a clearer description could be

Returns one instance of the entity 'MyEvent (B)' which are directly related to the (current) instance of the entity 'MyEvent (A)' where MyEvent(A).IdpreviousEvent=MyEvent(B).Idevent

This will make clear: *) which entity is the already loaded *) which entity will be loaded/returned *) how the loaded data will be used to build the filter to fetch the returned entity

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 10-Mar-2009 22:39:16   

Thanks for the repo solution - something a bit screwy is going on there. I'll get the developers to take a look.

Matt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 11-Mar-2009 11:20:19   

Seems like a bug in the template for the entity (C# and VB.NET). If you look at MyEventEntity.vb, method GetRelationsForField, you'll see that "PreviousEvent" has the same relationship as "NextEvent", which is wrong, as it should use MyEventEntity.Relations.MyEventEntityUsingIdevent.

The relation it has now, is for NextEvent. Due to this bug, the parent entities are seen as the PK side (which is wrong when you fetch previous events, it's the FK side) the merger merges the wrong pk-fk relationships.

Working on fix.

Frans Bouma | Lead developer LLBLGen Pro
Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 11-Mar-2009 11:24:49   

Otis wrote:

Working on fix.

Thank you simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 11-Mar-2009 11:36:10   

See attached template for the fix.

Frans Bouma | Lead developer LLBLGen Pro
Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 11-Mar-2009 12:06:54   

Otis wrote:

See attached template for the fix.

Perfect! Now it works as it should smile

Thank you very much, your support service is the best! sunglasses