Creating SubType when SuperType already exists.

Posts   
 
    
Angus
User
Posts: 44
Joined: 08-Jul-2005
# Posted on: 17-Nov-2005 22:55:08   

I want to create a sub type using the "Target-per-entity" hierarchies. I want to create the super type... save it .... add a sub type later.

Using the Example from the documentation. I create an Employee Enity and save it in the database., which saves it in the Employee table. Some time goes by and Employee is now promoted to a Manager.

I want to create a Manager entry in the 'Manager' table that has the PK /FK to the already created Employee. I want to use the existing employee ID (PK) because there are other relationships with other tables the are using the employee PK.

How is the best way to do this? I have tried creating a new Manager entity using the existing employee id as the PK but I get a duplicate key exception for the Employee Table when I try to save.

Thank you for any help you can give!

Chris sunglasses

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 18-Nov-2005 11:28:33   

Can't be done. You can't do this in code:


class A
{
}

class B:A
{
}

A a = new A();
B b = (B)a;

because a isnt a B, it's an A. You try to make an A a B, and that's not going to work. If you really want to do this, map ANOTHER entity on the Manager table, and use that one to save the manager part of the entity. It's a true hack, so I'd avoid it. The main issue is: if you want to change an entity's type at runtime, don't use that entity in an inheritance hierarchy, use a roleID flag or something like that.

Frans Bouma | Lead developer LLBLGen Pro
Angus
User
Posts: 44
Joined: 08-Jul-2005
# Posted on: 18-Nov-2005 17:30:48   

Otis wrote:

Can't be done. You can't do this in code:


class A
{
}

class B:A
{
}

A a = new A();
B b = (B)a;

......

Thank you for responding. But I'm not quite tyring to do it the way you have pointed out.

I'm just trying to follow the examples that are provided in the docs for LLBL Pro.

From a real world use of entities as they mature through time, an employee would be created and stored in the database. After a while (maybe years) that employee is promoted to manager. I just want to add a record in the manager table that refrences the employee. I don't want to delete the employee and create a new manager entity with the same data because the employee (over time) has lots of other references to other items in the database. I have tried code like this.....


class Employee
{
}

class Manager:Employee
{
}

Employee emp = new Employee();
emp.SetSomeData;
adapter.SaveEntity(emp);

///Some time passes... Years go by and the diligent employee gets promoted... :-)


Manager man = new Manager();
man.ID = emp.ID;  // (Keeping the same key so other references will not break)
man.Data = emp.Data;  //Manually map data from employee to manager
man.ExtraManagerData = SomeManagerData;
adapter.SaveEntity(man); //this will throw dup key exception.

I would like to somehow have the Generated classes be able to handle this situation. If adding a new Subtype, it could check if it needs to create entries in both tables, if so then do that, if the Supertype already exists then just create an instance of the sub type that references the Supertype.

I think that from a business (maybe not technical) point of view, this situation occurs quite often. It seams more natural (to me anyway) to follow the workflow I explained here.

Anyone else have thoughts on this??

Thanks for listening.

Chris

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 21-Nov-2005 11:19:19   

I understand your problem, and I can understand why you want to add 'just the manager record' to make the employee entity a manager.

If you want to do that, map another entity on 'manager' and save an instance of that entity, which should insert a new record in the manager table, adding the data to the employee entity.

The problem is though that if an instance of a type T can change semantically (!) over time to another type T2, T isn't suitable for inheritance. Another example is the dreaded 'Person' supertype and 2 subtypes: Customer and Employee and then the question pops up 'what about when an employee is also a customer?'. In a relational database this can be done by simply adding a customer record which points to the same person record as the employee record does.

Though when you do that, you effectively share data among two entities. To avoid that, you should use a different way to solve this: add a role type. For example in the Employee table, you have an EmployeeType field which illustrates the job type the employee has.

To use inheritance with a relational database has some drawbacks. This is one of them.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 1251
Joined: 10-Mar-2006
# Posted on: 27-Jul-2006 06:14:28   

If you want to do that, map another entity on 'manager' and save an instance of that entity, which should insert a new record in the manager table, adding the data to the employee entity.

I also need to do this. I still dont understand how to do it.

For me, the inheritance works good for the most part. However there are times where I need to make an Entity go from Employee to Manager or Manager to Employee.

If I just execute regular SQL - it is a simple one liner! Just delete the record from Manager to make them a regular Employee or add a record to Manager to make them into a Manager. It would work nicely.

The only problem I have, is understanding how to make your framework do this?

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 27-Jul-2006 06:54:16   

If you want to do that, map another entity on 'manager' and save an instance of that entity, which should insert a new record in the manager table, adding the data to the employee entity.

What Frans has suggested above is to use the LLBLGen Pro Designer to map anothe entity on the same manager table lets call it "PromotedToManager" which is not part of the inheritance hierarchy, then you can create and save an entity of this type which references the needed employee entity.

Posts: 1251
Joined: 10-Mar-2006
# Posted on: 27-Jul-2006 08:02:22   

Ah - thanks. I will do that.

Seems like support would be built in for this type of operation. Oh well, at least I have a workaround!