一对多映射关系未正确保存数据

问题描述 投票:0回答:1

我正在寻找很长时间来访问我的数据并将其发布到我的数据库中。我成功发送了报告的名称,但未发送所链接的章节。我对各个章节之间的审核有一对多的映射。感谢您的帮助,我希望我能说清楚^^“,我是法语和开发新手。我的资料库:Database有我的要求json为春季引导:

{
    "nameReport": "Title of the report",
    "chapters": [
        {
            "titleChap": "Title of the chapter",
            "descChap": "The description of the chapter"
        },
            "titleChap": "2nd Title of the chapter",
            "descChap": "2nd description of the chapter"
        }
    ]
}

Audit.java

@Entity
@Table(name="audits")

public class Audit {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", nullable = false, unique = true)
  private Integer id;

  @Column(name = "report_name")
  private String nameReport;

  @OneToMany(fetch = FetchType.LAZY, mappedBy="audit", cascade = CascadeType.ALL)
  private Collection<Chapter> chapters;

  public Audit() {
  }

  public Audit(String nameReport, Collection<Chapter> chapters) {
    this.chapters = new ArrayList<Chapter>();
    this.nameReport = nameReport;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getNameReport() {
    return nameReport;
  }

  public void setNameReport(String nameReport) {
    this.nameReport = nameReport;
  }

  public Collection<Chapter> getChapters() {
    return chapters;
  }

  public void setChapters(Collection<Chapter> chapters) {
    this.chapters = chapters;
  }

Chapter.java

@Entity
@Table(name="chapitres")

@Component
public class Chapter {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
//  @GeneratedValue
  @Column(name = "id")
  private Integer id;

  @ManyToOne(fetch = FetchType.LAZY, optional = false)
//  @ManyToOne
  @JoinColumn(name = "audit_id")
  private Audit audit;

  @Column(name = "chapter_title")
  private String titleChap;

  @Column(name = "chapter_desc")
  private String descChap;

  public Chapter() {}

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public Audit getAudit() {
    return audit;
  }

  public void setAudit(Audit audit) {
    this.audit = audit;
  }

  public String getTitleChap() {
    return titleChap;
  }

  public void setTitleChap(String titleChap) {
    this.titleChap = titleChap;
  }

  public String getDescChap() {
    return descChap;
  }

  public void setDescChap(String descChap) {
    this.descChap = descChap;
  }

auditController.java

@RestController
@RequestMapping(value = "/api")

public class AuditController {
  private AuditRepository auditRepository;

  @Autowired
  AuditController(AuditRepository auditRepository){
    this.auditRepository = auditRepository;
  }

  @PostMapping("/audits")
  public ResponseEntity<Audit> postAudit(@RequestBody Audit audit, @RequestBody Collection<Chapter> chapters) {
    try {
      Audit _audit = auditRepository.save(new Audit(audit.getNameReport(), audit.getChapters()));
      return new ResponseEntity<>(_audit, HttpStatus.CREATED);
    } catch (Exception e) {
      return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
    }
  }

  @GetMapping("/audits")
  public ResponseEntity<List<Audit>> getAllAudits() {
    List<Audit> audits = new ArrayList<>();
    try {
      auditRepository.findAll().forEach(audits::add);

      if(audits.isEmpty()) {
        return new ResponseEntity<>(audits, HttpStatus.NO_CONTENT);
      }
      return new ResponseEntity<>(audits, HttpStatus.OK);
    } catch (Exception e) {
      return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @GetMapping("/audits/{id}")
  public ResponseEntity<Audit> getAuditById(@PathVariable("id") Integer id) {
    Optional<Audit> auditData =  auditRepository.findById(id);

    if (auditData.isPresent()) {
      return new ResponseEntity<>(auditData.get(), HttpStatus.OK);
    } else {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
  }
}
java list spring-boot arraylist one-to-many
1个回答
0
投票

不确定这是否是解决方案

public Audit(String nameReport, Collection<Chapter> chapters) {
    this.chapters = new ArrayList<Chapter>();
    this.nameReport = nameReport;
  }

您将在此处创建新的数组列表,因此在Audit中不会添加章节。您应该将其更改为

 public Audit(String nameReport, Collection<Chapter> chapters) {
        this.chapters =chapters;
        this.nameReport = nameReport;
      }

0
投票
public Audit(String nameReport, Collection<Chapter> chapters) {
    this.chapters = new ArrayList<Chapter>();
    this.nameReport = nameReport;
}

您正在创建空的章节列表。

© www.soinside.com 2019 - 2024. All rights reserved.