FK of an entity being the AutoGenerated(PK) of another entity

Posts   
 
    
meetarifmd
User
Posts: 3
Joined: 13-May-2008
# Posted on: 19-May-2008 19:03:45   

Consider a DB Design where an EntityToBeSaved using adapter.SaveEntity. This EntityToBeSaved has collection of other sub-entities(to be saved in different table) and the relation of these subentities is like the field(FK) of one sub-entity relates to the autogeneratedKey(PK) of another sub-entity.

In normalway, without using genpro, we would go in these steps. 1) Save the one subentity and get its autogenerated(PK) 2) Relate FK of another subentity to this autogenerated(PK) to and save it 3) finally save EntityToBeSaved by relating to the above subentities.

Is there any way to tell genpro do this while creating these subentities and relating them with the EntityToBeSaved?

*FK : Foriegn Key *PK : Primary Key

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 19-May-2008 19:21:20   

Yes this is called Recursive Saves, this is quite straightforward wiht LLBLGenPro, you create your main entity, then create the collection of related entities and add this collection to your main entity, you can continue doing this several levels down, and in the end, the adapter.SaveEntity would save the whole graph and even use a Transaction to ensure your data gets properly persisted.

meetarifmd
User
Posts: 3
Joined: 13-May-2008
# Posted on: 19-May-2008 23:43:44   

Yes thats obsolutely correct, but thats not what my problem is.

My problem is that the sub-entities have relation like the field(FK) of one sub-entity relates to the autogeneratedKey(PK) of another sub-entity and both are "new-entities" and both are to be saved in one save call to genpro.

Forget about the hierarchy depth, what if we want an entitycollection to be saved where the entities relates like the field(FK) of one entity as the autogeneratedKey(PK) of another entity and both are "new-entities" then what shud one do?

Let me explain you with an example.

we have 2 XYZEntities to be saved as collection in one go Let these be XYZ1Entity and XYZ2Entity. Now the relation what i need is XYZ2Entity.FK = XYZ1Entity.PK (which is auogenerated when saved)

But then i dont know what "XYZ1Entity.PK" is and so i cannot make the above statement. So is there anyway genpro does it?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-May-2008 06:14:47   

The code is aware of sequences / identity columns and will automatically set the value for an identity / sequence column after the entity is physically saved inside SaveEntity(). The new value for sequenced columns is available to you after SaveEntity(), even though you haven't specified that the entity has to be refetched.

So, behind the scenes LLBLGen Pro Framework knows that it must save _XYZ1Entity _first and get its PK (autogenerated) then it set that PK to the _XYZ2Entity _FK and then save XYZ2Entity. If you stay the refetch parameter to the Adapter.SaveEntity/SaveEntityCollection call, you can see that the PK/FK synchronization is handled nicely by LLBLGen Pro Framework.

If you aren't experimenting this, maybe you have something set wrong somewhere. Could you confirm that everything works as I explained?

David Elizondo | LLBLGen Support Team