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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24445388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:14:24  来源:igfitidea点击:

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

javaspringhibernatejpaspring-data-jpa

提问by user3560205

Below is my table design. can someone explain me how to configure my entity using spring data jpa?

下面是我的桌子设计。有人可以解释我如何使用 spring 数据 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
)

回答by shazin

If you are asking for JPA Mapping then should be as following.

如果您要求 JPA 映射,则应如下所示。

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

回答by paul

@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 for the repository

对于存储库

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


      }