| Author |
Hibernate: table per class inheritance
|
vikas sharmaa
Ranch Hand
Joined: Jun 28, 2007
Posts: 191
|
|
Employee table is mapped to Employee class. Employee table has some records.
Now, requirement is to create child tables Regular_Emp and Contract_Emp.
So, using table per class inheritance strategy I have created these tables. both Regular_Emp and Contract_Emp extends Employee class.
Now, how could I write code to insert data in Regular_Emp or Contract_Emp tables corresponding to data present in parent table Employee. Please help.
|
 |
Wendy Gibbons
Bartender
Joined: Oct 21, 2008
Posts: 1098
|
|
|
as far as I am aware, as long as your hibernate files are set up properly, just by doing stuff to the employee the child tables should be updated automatically.
|
 |
vikas sharmaa
Ranch Hand
Joined: Jun 28, 2007
Posts: 191
|
|
Wendy,
If I write code like Employee emp = new RegularEmp(); and then set emp data; in that case both EMPLOYEE and REGULAR_EMP tables will be updated automatically.
But, in my case, EMPLOYEE table already have some data. and now, I am creating child tables REGULAR_EMP and CONTRACT_EMP. Now, I want to sync data in Employee table into REGULAR_EMP and CONTRACT_EMP.
I suppose, in such scenario I should go with one-to-one mapping. please correct me if I am missing some point.
|
 |
Mahendr Shinde
Ranch Hand
Joined: Sep 03, 2011
Posts: 37
|
|
If you are using Table per class it means Employee , Regular Employee and Contract Employee all must be created at same time.
It means EACH EMPLOYEE from employee table is either Regular or on contract.
I think you made Employee first and then added those two tables later. This is wrong approach!
|
There is still lot to learn!
|
 |
vikas sharmaa
Ranch Hand
Joined: Jun 28, 2007
Posts: 191
|
|
Mahendr Shinde wrote:I think you made Employee first and then added those two tables later. This is wrong approach!
this might be wrong approach for Hibernate inheritance mapping.
But, the scenario is:
I have an EMPLOYEE table with multiple columns. I execute many queries on EMPLOYEE table.
Now, there is an enhancement that is specific for few employees. These few Employees are categorised as REGULAR_EMP and CONTRACT_EMP. Please note that there can be Employees who are neither Regular nor Contract Emp.
Now, I don't want to do changes in existing EMPLOYEE table just for few Employees.
It seems I should go with one-to-one relationship instead of inheritance. am I right?
|
 |
Mahendr Shinde
Ranch Hand
Joined: Sep 03, 2011
Posts: 37
|
|
Yes, you are right.
One to One is better in this scenario.
|
 |
 |
|
|
subject: Hibernate: table per class inheritance
|
|
|