Refetching entities with identity sequence

Posts   
 
    
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 23-Apr-2012 10:54:38   

I found this line in the docs:

You don't need to refetch an entity if it has a sequenced primary key (Identity or sequence), as these values are read back directly with the insert statement.

I assume this means all fields but what about a hierarchy where the root has an identity sequence but the entity being saved doesn't - which fields are read back in that scenario?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 23-Apr-2012 10:59:48   

If you have: (Target per entity) Animal (Id (identity), name) <- Mammal (Age) <- Dog (Taillength)

and you save a new Dog entity, it will perform 3 inserts, where the first will obtain the Id value back and it will auto-sync this with the FK in Mammal (which is the PK of Mammal) ( and the FK in Dog (which is the PK in dog).

So it will first do INSERT INTO Animal (Name) VALUES (@p0); SELECT @p1= SCOPE_IDENTITY();

then it will sync the read value in p1 with the parameter set for the PK in Mammal and Dog and do: INSERT INTO Mammal (Id, Age) VALUES (@p0, @p1); where @p0 is the value read back from the Animal insert query

and INSERT INTO Dog (Id, Taillength) VALUES (@p0, @p1) where @p0 is the value read back from the animal insert query.

LLBLGen Pro doesn't read back the other values, only auto-number pk fields. It will sync fk fields automatically, also in hierarchies, so no extra work needed.

Frans Bouma | Lead developer LLBLGen Pro