【问题标题】:Insert parent and Child in Springboot through Postman通过 Postman 在 Springboot 中插入 parent 和 Child
【发布时间】: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


    【解决方案1】:

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

    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 所需的全部内容。但是通过这种方式,孩子不会有对其父母的引用。

    1. 双向
    @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);
    }
    

    【讨论】:

    • 感谢您的帮助,我仍然在外键中得到空值。你能在邮递员中测试它并分享你的代码吗?
    • 是的,基本上你可以试试我的代码。对于双向,无论何时添加PlannificationConcours,都不要忘记为它设置Concours,正如我上面提到的child.setConcours(this);。另一件事是,spring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql=true 在你的 application.properties(application.yml) 中检查它是否有一个 sql 查询来更新孩子的父母 ID。
    • 这是您在上面运行双向@Test 后将看到的。您会注意到,首先,它会插入您的Concours,其次,它将插入您的PlannificationConcoursconcours_id Hibernate:插入 concours 默认值 Hibernate:插入 plannification_concours (concours_id) 值(?) Hibernate:插入进入 plannification_concours (concours_id) 值 (?)
    • Parent 和 child 是根据下面的 sql 通知创建的,但没有应用关系。 Hibernate: select nextval ('pnu_concours_id_generator') Hibernate: select nextval ('pnu_plannificationconcours_id_generator') Hibernate: 插入 concours (date_debut, date_fin, description, id) 值 (?, ?, ?, ?) Hibernate: 插入 plannification_concours ("date ", concours_id, note_de_passage, quote, id) 值 (?, ?, ?, ?, ?)
    • 你是否在服务的某个地方调用了 addPlannificationConcours 的方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2019-01-09
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多