I am dealing with a situation where I have two classes, parent and child, with a self-referential relationship on the child side. The database is set up with separate tables for both parent and child, sharing the same "id", and using the column "holder" as a foreign key to itself. Below is the code that I have written. As I navigate this complex example of inheritance and interrelation, I am seeking guidance on whether my notations are correct.
Person.class
@Entity @Getter @Setter @NoArgsConstructor @Inheritance(strategy = InheritanceType.JOINED) @Table(name = "person") public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Integer id; @NotNull @Column(name = "name") private String name; }
Member.class
@Entity @Getter @Setter @NoArgsConstructor @Table(name = "member") public class Member extends Person { @NotNull @Column(name = "member_type") private Integer memberType; @Column(name = "id_holder") private Integer idHolder; @ManyToOne(targetEntity = Member.class) @JoinColumn(name = "id_holder", referencedColumnName = "id", insertable = false, updatable = false) private Member holder; @OneToMany(mappedBy = "holder", cascade = CascadeType.ALL) private List<Member> dependents; }
The object structure that will populate these entities on the frontend:
Member.model
export class Member { constructor( public id?: number, public name?: string, public memberType?: number, public idHolder?: any ) {} }