Polymorphic Saves- Adding a type that has multiple subtype instances

Posts   
 
    
cebus2000
User
Posts: 29
Joined: 11-Jan-2006
# Posted on: 30-May-2007 17:49:05   

We have possibly a unique situation we've gotten ourselves into. In our model, we have a base supertype called Component. This is represented by a single physical table. A given Component entity can have multiple subtypes. The hiearchy is based on propagating the primary key of the supertype table (ComponentId.) So, for a given (abstract) ComponentId, you could have 1 to N subtype instances. Each subtype lives in its own 'leaf' table (ComponentTypeA, ComponentTypeB, etc.)

This works beautifully doing polymorphic fetches, prefetches, etc. It's been a godsend.

The problem is performing INSERTs. If you have defined 3 subtype instances to be added, to be gathered under one supertype instance, looking something like this: MySuperType1- PK value=1 --MySubTypeA (PK == 1) + subtype fields --MySubTypeB (PK==1) + other subtype field --MySubTypeC (PK ==1) + yet more subtype fields

We can't figure out how to actually save this... If we loop thru the subtype instances, the first entity (subtype and supertype) saves correctly. However, after various different approaches, for each additional subtype entity, we either get PK violations in the supertype table, or we end up with 3 different supertype entities saved.

We could write stored procs or flatten tables, but this would basically require scrapping our project and starting over (since we have several hundred of these tables to support.)

i tried to make this a generic question, but of course i can give nasty specific details if desired (fair warning!) smile

Thanks much!

(using LLBLGen 2.0, .Net 2.0 libs, assembly ver 2.0.50727, built-in Adapter templates, SQL Server 2005)

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-May-2007 02:55:20   

Hi, Could you please post the save routine code snippet you are using?

David Elizondo | LLBLGen Support Team
cebus2000
User
Posts: 29
Joined: 11-Jan-2006
# Posted on: 31-May-2007 17:33:50   

At the moment, there is no viable code snippet- I have no idea how to actually do this. I am attaching the lgp file with a sample of the model.

The problem is, we are using target-per-entity, and it's coming into conflict with the relational part of the model. In the attached project, a particular entity of type ManufacturedProduct can be (has an IS-A relationship) with 3 different subtypes (ThermalBarrier, BoardStock, CoverBoard.) In the DB, it's a one-many relationship, but target-per-entity is treating each subtype as a separate instance. So when we save, we either would try to loop thru each subtype and save, which throws a SQLException for PK violation, or we would try to 'append' the subtype instances to the 'root' entity (but we don't know how to code that scenario.)

Looking at the attached lgp file, what we are essentially trying to do is create a new ManufacturedProduct entity that has the three subtypes as 'facets,' populate them, and save (using Adapter.SaveEntity()). This works fine for fetching, but we're stuck on how to code this for Saves. I'm trying to see if conceptually this is possible with the built-in Adapter template.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Jun-2007 09:51:21   

It all comes to this line:

for a given (abstract) ComponentId, you could have 1 to N subtype instances.

This can not be true for Inheritace relations. This is pure Refrential Relation.

For an Inheritace Hierarchy, an instance of the SuperType, can have only one instance of a subType.

cebus2000
User
Posts: 29
Joined: 11-Jan-2006
# Posted on: 01-Jun-2007 12:56:33   

OK, fair enough. I think we can find a way to modify the code to support this, just wanted to see if it was necessary.

Thanks for the info!