Hello I am searching for a long time for accessing my data and post it into my database. I got succeed to send the name of my report but not the chapter who are linked to. I have Audits to Chapters mapped in one to many relations.
Now I have an exception in spring boot who saying :
The field 'audit_id' can't be empty (null)
I already fall on it but I don't know how to manage this exception...
I think there is a problem of the order of the insertion into mysql. The audit part need to be posted first and then chapter for instantiate the audit_id to the audit. I try the annotation Order but isn't the way or I don't use it well.
Thanks for help
There is my request json for spring boot:
"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"
}
]
}
Below are code samples.
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;
//Embedded va lier le contenu de la class chapters
// @Embedded
//List va selectionner le tableau json
// private List<Chapters> chapters;
@OneToMany(fetch = FetchType.LAZY, mappedBy="audit", cascade = CascadeType.ALL)
private Collection<Chapter> chapters;
public Audit() {
}
public Audit(String nameReport, Collection<Chapter> chapters) {
this.chapters = chapters;
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) {
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);
}
}
}