Reputation: 4475
Lets say, I have a class Employee
with the fields employeeId
and name
. This is inherited by another class PermanentEmployee
with the field salary
and experience
. I used @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
.
From this I understand that it'll create two tables. one for the Parent class with two columns employeeId
and name
. Another table for the child class with four columns. That is name
,employeeid
,salary
and experience
.
When i try to persist the child object (Permanent Employee) with all four values, it is inserting in the child table. Only in the child table. It's not inserting in the parent table.
[1] Is this is the default behaviour? or am i missing anything? do i need entity relationships like @OneToOne
mapping?
[Side note] I also tried with @Inheritance(strategy = InheritanceType.JOINED)
. This time with Table per subclass, it is creating two tables and foreign key relations for them. And Its inserting in both the table.
Upvotes: 1
Views: 532
Reputation: 28136
Yes, if you are using @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
, the entity will be persisted into a single table, which represents the class of persisted Entity. It could be good for perfomance, because you don't have to make a table joins, while getting an Entity instance. But it also has a disadvantages, for example, the tables are not normalized, modification of parent in hierarchy causes changes in all the child tables. In most cases, this strategy is not recomended, though it is the simpliest one.
As forOneToOne
, if you mean, that you have to provide relationship between parent class and it's subclasses, then no. It is used to make a relationship between two or more instances, not for the class hierarchy.
Upvotes: 1