Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: table per class inheritance

 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right.
One to One is better in this scenario.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic