简体   繁体   English

JPA 实体外键是来自其他实体的主键

[英]JPA entity foreign key is a primary key from other entity

I'm sorry if this post title is not understandable如果这篇文章标题不能理解,我很抱歉

I have several tables in a Oracle database and I want to generate each entity in Spring Boot JPA.我在 Oracle 数据库中有几个表,我想在 Spring Boot JPA 中生成每个实体。

This is a piece of I have:这是我拥有的一块:

CREATE TABLE T1 (
   C1 NUMBER(4) NOT NULL,
   C2 NUMBER(5) NOT NULL,
   C3 NUMBER(2) NOT NULL,
   C4 NUMBER(9) NOT NULL,
   C5 VARCHAR2(10 CHAR) NOT NULL,
   CONSTRAINT PK_T1 PRIMARY KEY (C1,C2,C3,C4)
);

CREATE TABLE T2 (
   C1 NUMBER(4) NOT NULL,
   C2 NUMBER(5) NOT NULL,
   C3 NUMBER(2) NOT NULL,
   C4 NUMBER(9) NOT NULL,
   C5 NUMBER(6) NOT NULL,
   C6 NUMBER(3) NOT NULL,
   C7 VARCHAR2(10 CHAR) NOT NULL,
   C8 VARCHAR2(20 CHAR) NOT NULL,
   CONSTRAINT PK_T2 PRIMARY KEY (C1,C2,C3,C4,C5,C6),
   CONSTRAINT FK_T2_T1 FOREIGN KEY (C1,C2,C3,C4) REFERENCES T1
);

I need to map this both tables to JPA entities but I can't figure out how to map them.我需要将这两个表映射到 JPA 实体,但我不知道如何映射它们。

These are the entities classes in Java:这些是 Java 中的实体类:

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Table(name = "T1")
@Data
public class T1Bean implements Serializable {
   @EmbeddedId
   private T1PK pk;
   @Basic
   @Column(name = "C5", nullable = false)
   private String c5;
}
------------------
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import lombok.Data;
@Data
@Embeddable
public class T1PK implements Serializable {
   @Basic
   @Column(name = "C1", nullable = false)
   private int c1;
   @Basic
   @Column(name = "C2", nullable = false)
   private int c2;
   @Basic
   @Column(name = "C3", nullable = false)
   private int c3;
   @Basic
   @Column(name = "C4", nullable = false)
   private long c4;
}
------------------
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Table(name = "T2")
@Data
public class T2Bean implements Serializable {
   @EmbeddedId
   private T2PK pk;
   @Basic
   @Column(name = "C7", nullable = false)
   private String c7;
   @Basic
   @Column(name = "C8", nullable = false)
   private String c8;
   @OneToOne
   private T1Bean t1Bean;
}
------------------
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import lombok.Data;
@Data
@Embeddable
public class T2PK implements Serializable {
   @Basic
   @Column(name = "C1", nullable = false)
   private int c1;
   @Basic
   @Column(name = "C2", nullable = false)
   private int c2;
   @Basic
   @Column(name = "C3", nullable = false)
   private int c3;
   @Basic
   @Column(name = "C4", nullable = false)
   private long c4;
   @Basic
   @Column(name = "C5", nullable = false)
   private int c5;
   @Basic
   @Column(name = "C6", nullable = false)
   private int c6;
}

Which annotation is missing in T2Bean entity to make looks good the mapping between each entity? T2Bean 实体中缺少哪个注释以使每个实体之间的映射看起来不错?

I only put the @OneToOne annotation but I don't know if is correct.我只放了@OneToOne 注释,但我不知道是否正确。

First you just need to map two classes T1 and T2 these classes are considered as Bean or module or Entity首先你只需要映射两个类 T1 和 T2 这些类被认为是 Bean 或模块或实体

Entity T1实体T1

@Entity
@Table(name = "T1")
@Data
public class T1 implements Serializable {
@EmbeddedId
private <type> c1;
private <type> c2;
private <type> c3;
private <type> c4;

private T2 t2;
/* getters & setters */
@Column(name = "C1", nullable = false)
public String getC1(){
    return c1;
}

public void setC1(c1){
    this.c1 = c1;
}

// .... All other classes are the same

@OneToOne(fetch = FetchType.EAGER, targetEntity = T2.class)
public T2 getT2(){
    return t2;
}

public void setT2(t2){
    this.t2 = t2;
}

Entity T2实体T2

@Entity
@Table(name = "T2")
@Data
public class T2 implements Serializable {
@EmbeddedId
private <type> c1;
private <type> c2;
private <type> c3;
private <type> c4;
private <type> c5;
private <type> c6;
private <type> c7;
private <type> c8;

private T1 t1;
/* getters & setters */
@Column(name = "C1", nullable = false)
public String getC1(){
    return c1;
}

public void setC1(c1){
    this.c1 = c1;
}

//Foreigin key 
@OneToOne(fetch = FetchType.EAGER, targetEntity = T1.class)
@JoinColumn(name ="c1", nullable = false)
public T1 getT1(){
    return t1;
}

public void setT1(t1){
    this.t1 = t1;
}

Thanks to the @chris comment .感谢@chris 评论

I found the solution for T2Bean entity as below:我找到了 T2Bean 实体的解决方案,如下所示:

@Entity
@Table(name = "T2")
@Data
public class T2Bean implements Serializable {
   @EmbeddedId
   private T2PK pk;
   @Basic
   @Column(name = "C7", nullable = false)
   private String c7;
   @Basic
   @Column(name = "C8", nullable = false)
   private String c8;
   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumns({
         @JoinColumn(name = "c1",referencedColumnName = "c1",insertable = false,updatable = false),
         @JoinColumn(name = "c2",referencedColumnName = "c2",insertable = false,updatable = false),
         @JoinColumn(name = "c3",referencedColumnName = "c3",insertable = false,updatable = false),
         @JoinColumn(name = "c4",referencedColumnName = "c4",insertable = false,updatable = false),
   })
   private T1Bean t1Bean;
}

And the @Embeddable for T1 with the annotation @PrimaryKeyJoinColumn .以及带有注释@PrimaryKeyJoinColumn 的T1 的@Embeddable

@Data
@Embeddable
@PrimaryKeyJoinColumn
public class T1PK implements Serializable {
   @Basic
   @Column(name = "C1", nullable = false)
   private int c1;
   @Basic
   @Column(name = "C2", nullable = false)
   private int c2;
   @Basic
   @Column(name = "C3", nullable = false)
   private int c3;
   @Basic
   @Column(name = "C4", nullable = false)
   private long c4;
}

With that entity, 2nd request doesn't work:使用该实体,第二个请求不起作用:

REQUEST 1:请求 1:

{
    "pk": {
        "c1": 1,
        "c2": 2,
        "c3": 3,
        "c4": 998755544548
    },
    "c5": "John"
}

REQUEST 2:请求 2:

{
    "pk": {
        "c1": 1,
        "c2": 2,
        "c3": 5,
        "c4": 998755544548,
        "c5": 55,
        "c6": 15
    },
    "c7": "Simon",
    "c8": "system"
}

暂无
暂无

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

相关问题 向复合主键添加外键,并在JPA Entity类中进行更改 - Add a foreign key to composite primary key and changes in JPA Entity class JPA eclipselink坚持没有主键但有两个外键的实体 - JPA eclipselink persist entity that has no primary key but two foreign keys JPA 实体没有主键? - JPA entity has no primary key? JPA映射:关联哪个主键是一个实体的复合主键和另一个实体的一个属性的复合 - JPA mapping: Assositation which primary key is compound of composite primary key of one entity and one atribute of other entity 为具有主键的实体定义弹簧数据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 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? 如何使一个实体作为另一个实体的主键 - How to make an entity as a primary key for the other entity 父实体的主键未在子实体中存储为外键 - Primary key of Parent Entity not stored as Foreign Key in Child Entity Spring Data JPA:无法使用包含外键的复合主键保存实体 - Spring Data JPA: Cannot save entity with composite primary key which contains foreign key Spring data jpa 在空数据库中为具有包含外键的复合主键的实体创建错误的字段 - Spring data jpa creates wrong fields in empty database for entity with composite primary key that contains foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM