Save order for collections

Posts   
 
    
evb
User
Posts: 27
Joined: 17-Nov-2004
# Posted on: 29-Jan-2008 15:16:35   

Hi,

Imagine you have a filled-in collection and you want to save that collection to the database. Is the save of each entity done in the same order as in the collection?

Example : Collection entity_0 entity_1 entity_2

save to database first the entity_0, then the entity_1 and then entity_2?

What if you do a sort on the collection (client sorting) and the order is changing.

Example : Collection entity_2 entity_0 entity_1

save to database first the entity_2, then the entity_0 and then entity_1?

If above is not true, can we use the UnitOfWork2 object? Must I translated the collection to an add one for one of a entity to a unitofwork object?

I must be able to save a collection in a specific order to the database table.

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 29-Jan-2008 17:31:10   

I actually didn't know this because is very unusual to care about the order of the saving of an entity collection, so I create a little test solution and come to the conclusion that yes, the SaveCollection method would save the entities in the order that you sort it. if you don't apply any sort to the collection they would be saved in order of creation.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 29-Jan-2008 21:12:52   

There's a catch: if inside the collection there are dependencies (e.g. you have a collection of employees and some employee entities refer to eachother), then these are sorted in the right order. So if there are no dependencies inside the collection, no problem, otherwise the order is the one which is actually the order in which entities are saveable simple_smile

Frans Bouma | Lead developer LLBLGen Pro
evb
User
Posts: 27
Joined: 17-Nov-2004
# Posted on: 30-Jan-2008 08:22:14   

Ok, thanks for your answers.

The reason for the specific save order are triggers on the tables who are verifying if the new added record doesn't exist already in the datatable. If yes the new added record is deleted... These are timeline aware tables, my record is "valid from" till another record "valid from" with other values. If I have a record valid from 01/01/2000 with value 1 and I add a new record valid from 01/01/2007 with value 1, then this last one is unnecessary! It isn't changing anything.