繁体   English   中英

Spring DATA JPA示例用于单个实体中的多个外键

[英]Spring DATA JPA example for multiple foreign keys in a single entity

下面是我的桌子设计。 谁能解释我如何使用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;


}

而对于存储库

     @Repository
     public interface ParentRepository extends CrudRepository<Parent, Integer> {


      }

如果您要求JPA Mapping,那么应该如下。

@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;
}

@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;

    @OneToOne
    @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;

    @OneToOne
    @JoinColumn(name="parent_id", referencedColumnName="ID")
    private Parent parent;

    @OneToOne
    @JoinColumn(name="first_child_id", referencedColumnName="ID")
    private FirstChild firstChild;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM