简体   繁体   English

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

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

Below is my table design. 下面是我的桌子设计。 can someone explain me how to configure my entity using spring data jpa? 谁能解释我如何使用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 for the repository 而对于存储库

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


      }

If you are asking for JPA Mapping then should be as following. 如果您要求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.

相关问题 Spring Data JPA:如何在插入期间在子实体上设置 2 个外键 - Spring Data JPA: How to set 2 foreign keys on the child entity during insert 有没有办法使单个 JPA 实体中的两个外键组合唯一? - Is there a way to make the combination of two foreign keys in a single JPA entity unique? 使用两个外键创建一个实体 Class - Spring JPA - Create an Entity Class with two foreign keys - Spring JPA 我可以使用 Spring Data JPA 处理多个实体类的单个 JPA 存储库接口吗? - Can I have a single JPA repository interface handling multiple entity classes using Spring Data JPA? 一个映射类Hibernate + JPA + Spring中有多个外键 - Multiple foreign keys in one mapping class Hibernate+JPA+Spring 设计带有 2 个外键的 Spring Data jpa 注释类 - design spring data jpa annotated class with 2 foreign keys 在JPA 2.0中从没有外键的多个表中获取数据 - Getting data from multiple tables without foreign keys in JPA 2.0 JPA 2 - 如何使用Spring Data JPA构建具有主键的实体,该主键也是外键? - JPA 2 - How to build entity that has Primary key that is also a foreign key using Spring Data JPA? 为具有主键的实体定义弹簧数据JPA存储库接口,该主键也是JPA 2.x中的外键 - Defining a spring-data JPA repository interface for entity with primary key that is also a foreign key in JPA 2.x Spring Data JPA获取实体外键而不会导致从属实体延迟加载 - Spring Data JPA get entity foreign key without causing the dependent entity lazy load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM