Home > database >  Is there a way to share the parent id with the child entities in hibernate?
Is there a way to share the parent id with the child entities in hibernate?

Time:09-30

I would like to share the parent ID with the child entities.

So I would like to achieve something like this:

parent table
id | childName | ...
1  | childA    | ...
2  | childB    | ...

childA table
id | childASpecific | ...
1  | ASpecificValue | ...

childB table
id | childBSpecific | ...
2  | BSpecificValue | ...

Is there a way to do this in hibernate?

CodePudding user response:

In the meantime, I found the solution:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Parent {
    @Id
    private Long id;
    private String childName;

    // constructor, getters, setters
}
@Entity
public class ChildA extends Parent {
    private String childASpecific;

    // constructor, getters, setters
}
@Entity
public class ChildB extends Parent {
    private String childBSpecific;

    // constructor, getters, setters
}
  • Related