通过Postman在Springboot中插入父级和子级



我想在一对多关系中保存一条带有spring boot through postman的记录及其子列表。子列表被保存,但它们不会自动获取父列表的Id。我怎么能强迫孩子自动采取父母的邮政请求在邮差的id ?

父类

package fdsa.edu.pnu.Model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Data
@Table(name = "Concours")
public class Concours implements Serializable {
@Column(name = "ID", nullable = false, length = 10)
@Id
@GeneratedValue(generator = "PNU_CONCOURS_ID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name = "PNU_CONCOURS_ID_GENERATOR", strategy = "native")
private Integer id;
@Column(name = "DateDebut", nullable = true)
@Temporal(TemporalType.DATE)
private java.util.Date DateDebut;
@Column(name = "DateFin", nullable = true)
@Temporal(TemporalType.DATE)
private java.util.Date DateFin;
@Column(name = "Description", nullable = true, length = 255)
private String description;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "concours",
cascade = CascadeType.ALL,targetEntity = fdsa.edu.pnu.Model.PlannificationConcours.class)
private List<PlannificationConcours> plannificationConcourses;

}

子类


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "PlannificationConcours")
public class PlannificationConcours implements Serializable {
@Column(name = "ID", nullable = false, length = 10)
@Id
@GeneratedValue(generator = "PNU_PLANNIFICATIONCONCOURS_ID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name = "PNU_PLANNIFICATIONCONCOURS_ID_GENERATOR", strategy = "native")
private int id;
@ManyToOne(targetEntity = fdsa.edu.pnu.Model.Concours.class, fetch = FetchType.LAZY)
@JoinColumns(value = {@JoinColumn(name = "ConcoursID", referencedColumnName = "ID")}, foreignKey = @ForeignKey(name = "ConcoursPlannificationConCours"))
private Concours concours;
@Column(name = "`Date`", nullable = true)
@Temporal(TemporalType.DATE)
private java.util.Date Date;
@Column(name = "Quotation", nullable = true, length = 10)
private double quotation;
@Column(name = "NoteDePassage", nullable = true, length = 10)
private double noteDePassage;

}```
Screen Shote where the Id of the parent is null
[![enter image description here][1]][1]

[1]: https://i.stack.imgur.com/LlnhP.png

有两种方法可以到达它。这是最小的设置:

  1. 单向
@Entity
@NoArgsConstructor
@Getter
@Setter
public class Concours {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "concours_id") // this line will play the role that passes the parent's id to its children
private List<PlannificationConcours> plannificationConcourses = new ArrayList<>();
}
@Entity
@NoArgsConstructor
@Getter
@Setter
public class PlannificationConcours {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
}
@Test
void saveConcours() {
Concours concours = new Concours();
concours.setPlannificationConcourses(List.of(new PlannificationConcours(), new PlannificationConcours()));
this.concoursRepository.save(concours);
}

这是传播父节点id所需要的全部内容。但是通过这种方式,子进程不会有对父进程的引用。

  • 双向
  • @Entity
    @NoArgsConstructor
    @Getter
    public class Concours {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @OneToMany(mappedBy = "concours" ,cascade = CascadeType.ALL) // mappedBy will create a bidirectional relation for us
    private List<PlannificationConcours> plannificationConcourses = new ArrayList<>();
    public void addPlannificationConcours(PlannificationConcours child) {
    child.setConcours(this); // and don't forget to set the parent instance to the child
    this.plannificationConcourses.add(child);
    }
    }
    
    @Entity
    @NoArgsConstructor
    @Getter
    @Setter
    public class PlannificationConcours {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @ManyToOne
    private Concours concours;
    }
    
    @Test
    void saveConcours() {
    Concours concours = new Concours();
    concours.addPlannificationConcours(new PlannificationConcours());
    concours.addPlannificationConcours(new PlannificationConcours());
    this.concoursRepository.save(concours);
    }
    

    最新更新