java - Spring DATA JPA example for multiple foreign keys in a single entity -
below table design. can explain me how configure entity using spring data jpa?
parent_table( id primary key, name ) second_child_table( id primary key, second_child_name, parent_id references id on parent_table, first_child_id references id on first_child_table ) first_child_table( id primary key, first_child_name, parent_id references id on parent_table )
@entity @table(name="parent_table") public class parent { @id @column(name="id", nullable=false, unique=true) // require generator config private long id; @column(name="name", nullable=false) private string name; @onetomany(orphanremoval = true, cascade = {cascadetype.all}, fetch = fetchtype.lazy) @joincolumn(name = "candidacy_id", nullable = false) @getter @setter private list<firstchild> firstchild = new arraylist<>(); @onetomany(orphanremoval = true, cascade = {cascadetype.all}, fetch = fetchtype.lazy) @joincolumn(name = "candidacy_id", nullable = false) @getter @setter private list<secondchild> secondchild = new arraylist<>(); } @entity @table(name="first_child_table") public class firstchild { @id @column(name="id", nullable=false, unique=true) // require generator config private long id; @column(name="first_child_name", nullable=false) private string name; @manytoone @joincolumn(name="parent_id", referencedcolumnname="id") private parent parent; } @entity @table(name="second_child_table") public class secondchild { @id @column(name="id", nullable=false, unique=true) // require generator config private long id; @column(name="second_child_name", nullable=false) private string name; @manytoone @joincolumn(name="parent_id", referencedcolumnname="id") private parent parent; }
and repository
@repository public interface parentrepository extends crudrepository<parent, integer> { }
Comments
Post a Comment