【发布时间】:2021-12-06 20:15:32
【问题描述】:
我想通过邮递员以一对多的关系使用 Spring Boot 保存记录及其子列表。子列表已保存,但它们不会自动获取父级的 ID。如何强制孩子在 Post Request In Postman 中自动获取父母的 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
【问题讨论】:
标签: java spring spring-boot jpa spring-data-jpa