Create a subentity from an superentity

Posts   
 
    
Courela
User
Posts: 3
Joined: 05-Jan-2006
# Posted on: 05-Jan-2006 17:58:25   

Hi, i'm trying to make an entity (call it subentity), which inherits from another (superentity) that already exists. Say i have an entity "User", already stored in database and i what to create an entity "FormsDesigner", which inherits from "User". The identifier for "User" is an ID, an auto generated integer, which is the foreign key of "FormsDesigner". When i try to create a new "FormsDesigner" with the same ID of "User", it tries to create another "User" with the same ID, so no can do.

Something like this

UserEntity user = new UserEntity(id);


FormsDesignerEntity forms = new FormsDesignerEntity();
forms.userId = user.Id;
forms.Save();

What i'm trying to say is that the "User" that already exists is now a "FormsDesigner". How do i do this?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Jan-2006 06:33:53   

I think you are mixing the "is a" relation with a "has a" relation (Inheritance versus composition).

If you want to follow an inheritance scenario, then FormsDesignerEntity PK should be the FK to the UserEntity PK.

Then you may do this:

UserEntity user = new FormsDesignerEntity(id);
OR
FormsDesignerEntity forms = new FormsDesignerEntity(id);
Courela
User
Posts: 3
Joined: 05-Jan-2006
# Posted on: 09-Jan-2006 15:55:04   

Walaa wrote:

If you want to follow an inheritance scenario, then FormsDesignerEntity PK should be the FK to the UserEntity PK.

Yes, that's the way it is done.

I've tried both statements but that didn't work. After getting the entity from de persistent storage, i tried to save the entity hoping that it created a new entry in the FormsDesigner table.


UserEntity user = new FormsDesignerEntity(id);
user.Save();
OR
FormsDesignerEntity forms = new FormsDesignerEntity(id);
forms.Save();

Both solutions return a new entity (IsNew = true) with the id set to the respective number. The Save() method tries to create a new entry in both tables, User and FormsDesigner. I only want a new entry in the FormsDesigner table.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39932
Joined: 17-Aug-2003
# Posted on: 09-Jan-2006 16:54:35   

That's not supported as that would make a has-a relationship to become an is-a relationship. You cant share data of 2 entities like the way you want it to do: The user entity data belongs to a single entity not to 2. If that would be done, deleting one would also delete data of the other which is not what you want. (and a ref-counter has to be introduced etc...) If you want to use it this way, don't use inheritance in this scenario.

Frans Bouma | Lead developer LLBLGen Pro