Collection Entity State

Posts   
 
    
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 07-Sep-2005 23:38:07   

Hi there,

I got an entity that is referencing itself by a property called IdFather.

MyEntity -- IdFather ---> MyEntity

I also have a property called Level that returns either 1 if the Entity is at the very root position or FatherEntity.Level +1 if not at the root. I use that to make creation of a hierarchy tree faster by sorting by the Level property from smallest to biggest.

When I fetch a MyEntityCollection from the db and change some entities fathers, the Level should by recalculated as written above. But what I experienced, is, that in the Collection only the entities level that has gotten a new father is being correctly computed, the childs Levels remain unchanged.

I am really confused...

Regards, André

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 08-Sep-2005 02:29:34   

Post some of your code for the update and we can help troubleshoot the problem. How are you doing the recalculation of the level?

Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 08-Sep-2005 12:51:18   

I simply change the Father property of an entity instance(Classname is Category).

And the Level is just a simple Property:


public override Int32 Lvl {
    get {
        if(Token == "ALL") {
            return 1;
        } else {
            return Category.Lvl+1;
        }
    }
    set {
        base.Lvl = value;
    }
}

The token is "ALL" when the Entity is at the very root level.

Initially all Entities are fetched into a CategoryCollection then I change the Father property from one Entity inside that collection. After that I enumerate the Collection and look at every entity. What happens is that only the entity that got a new father has a new level, all of its children remain at the same level.

Regards, André

Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 08-Sep-2005 16:49:14   

What I discovered is the following.

Example:

A Collection has some Entities... [1] [2] [3] [4]

those Entities can have self references using a Father property like...

3.Father = 2; 2.Father = 1

Now I make a change like:

2.Father = 4

taking out [3] from the Collection and browsing up the hierarchy, results in...

3's father is 2. 3's father's father is 1. Thats plain wrong, because 3's father's father(which is 2) should be 4 now!

and here comes the weird part.

Now I get 2 from the COLLECTION and not from the father reference of 3: 2's father is 4.

Now that's correct.

Seems like the hierarchy is not being updated down to the end, just one level.

I hope somebody could follow that example wink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 09-Sep-2005 10:46:09   

Jazz wrote:

What I discovered is the following.

Example:

A Collection has some Entities... [1] [2] [3] [4]

those Entities can have self references using a Father property like...

3.Father = 2; 2.Father = 1

Now I make a change like:

2.Father = 4

That's an object reference set action? (myOrder.Customer = myCustomer) ?

taking out [3] from the Collection and browsing up the hierarchy, results in...

3's father is 2. 3's father's father is 1. Thats plain wrong, because 3's father's father(which is 2) should be 4 now!

Who says 3 should be removed from the collection? You just set some entity values, the collection is a fetch result. Or I might not grasp what you're doing. Setting 2.Father = 4, will dereference 1 from 2, and will make 2.Father = 4. After that set action, is that indeed the case?

and here comes the weird part.

Now I get 2 from the COLLECTION and not from the father reference of 3: 2's father is 4.

Now that's correct.

Seems like the hierarchy is not being updated down to the end, just one level.

I hope somebody could follow that example wink

Ah, I think I know what's going on! simple_smile . You're using selfservicing? You fetch a single entity collection, and 3.Father isn't set to a reference, so you touch that, and it's read from the db, due to lazy loading.

Frans Bouma | Lead developer LLBLGen Pro
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 09-Sep-2005 12:24:32   

1) using Object reference, yes

2) actually "taking out" should be "taking a look at"... nothing to remove here simple_smile

3) before changing any parent-references. i bind the collection to a treeview, so for my understanding all objects should have been fetched. but now i think i got your point.

when I move at the top collection level (talking about hierarchy) +1 level down/up, everything is okay, but when it comes to get the father of a father the objects get refetched from the database? disappointed

Regards, André

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 10-Sep-2005 12:56:15   

Jazz wrote:

1) using Object reference, yes

2) actually "taking out" should be "taking a look at"... nothing to remove here simple_smile

3) before changing any parent-references. i bind the collection to a treeview, so for my understanding all objects should have been fetched. but now i think i got your point.

when I move at the top collection level (talking about hierarchy) +1 level down/up, everything is okay, but when it comes to get the father of a father the objects get refetched from the database? disappointed

You fetch 1, 2, 3 and 4. All of them don't have their parent set, as you just fetch a non-hierarchical set of entities. Now, you set 2.Father to 4. This is great, but 3.Father is still null/not read. You then do: 3.Father.Father.

This first hits the property of 3: 3.Father. As it's not read yet, it will be read from the db through lazy loading. 3.Father is 2 in the database, so a new 2 entity is read. This new 2 entity doesn't have its Father object set (the 2 object in your collection does) and this 2.Father will load itself from the db as well.

To avoid this, add the collection you're fetching to a Context. Using a Context, you can have uniquing, which means if you load an entity twice, it's actually loaded once and you have 2 references to the same object. As soon as you add the collection to a Context, 3.Father will still load itself from the db, but because 3 is in a context, it will check if teh loaded related entity, 2, is in the context. It is, so you get the 2 instance in the collection (which is in the context) set as 3.Father, and that 2 instance has its father set to 4.

Please see the Context documentation in the manual, it will solve your problem simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 10-Sep-2005 13:55:54   

That's it! Good example for the context. simple_smile Thanks.