Я пытаюсь создать API для сохранения объекта Pays, дело в том, что этот объект содержит внешний ключ для другого класса Region. когда я создал API для сохранения объекта региона, он работал, но когда я хочу сохранить новый экземпляр Pays, внешний ключ принимает значение NULL в базе данных.
платит класс:
@Entity
public class Pays {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id_pays;
private String pays;
private String description;
private String etat;
@ManyToOne
@JoinColumn(name="region_id")
@JsonBackReference
private Region region;
@OneToMany(mappedBy="pays",
cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
@JsonManagedReference
private List<Zone> zone;
public int getId_pays() {
return id_pays;
}
public void setId_pays(int id_pays) {
this.id_pays = id_pays;
}
public List<Zone> getZone() {
return zone;
}
public void setZone(List<Zone> zone) {
this.zone = zone;
}
public String getPays() {
return pays;
}
public void setPays(String pays) {
this.pays = pays;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getEtat() {
return etat;
}
public void setEtat(String etat) {
this.etat = etat;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
@Override
public String toString() {
return "Pays [idpays=" + id_pays + ", pays=" + pays + ", description=" + description + ", etat=" + etat + ", region="
+ region + ", zone=" + zone + "]";
}
}
региональный класс:
@Entity
public class Region {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id_Region;
private String region;
private String description;
@OneToMany(mappedBy="region",
cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
@JsonManagedReference
private List<Pays> pays;
public List<Pays> getPays() {
return pays;
}
public void setPays(List<Pays> pays) {
this.pays = pays;
}
public int getIdRegion() {
return id_Region;
}
public void setIdRegion(int idRegion) {
this.id_Region = idRegion;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void add(Pays tempPays) {
if (pays==null) {
pays=new ArrayList<Pays>();
}
pays.add(tempPays);
tempPays.setRegion(this);
}
@Override
public String toString() {
return "Region [idRegion=" + id_Region + ", region=" + region + ", description=" + description + ", pays=" + pays
+ "]";
}
}
класс paysContoller:
@RestController
public class PaysController {
@Autowired
private PaysService paysService;
//Get all the pays
@GetMapping("/api/pays")
public ResponseEntity<List<Pays>> list(){
List<Pays> list = paysService.listPays();
return ResponseEntity.ok().body(list);
}
//Save the pays
@PostMapping("/api/pays")
public ResponseEntity<?> save(@RequestBody Pays pays){
long id = paysService.save(pays);
return ResponseEntity.ok().body("Pays added successfully with id " + id);
}
//Get a single record
@GetMapping("/api/pays/{id}")
public ResponseEntity<Pays> get(@PathVariable("id") int id){
Pays pays = paysService.getPays(id);
return ResponseEntity.ok().body(pays);
}
//Updating a record
@PutMapping("/api/pays/{id}")
public ResponseEntity<?> update(@PathVariable("id") int id, @RequestBody Pays pays){
paysService.updatePays(id, pays);
return ResponseEntity.ok().body("Pays has been updated.");
}
//delete a record
@DeleteMapping("/api/pays/{id}")
public ResponseEntity<?> delete(@PathVariable("id") int id){
paysService.deletePays(id);
return ResponseEntity.ok().body("record deleted");
}
}
JSON parse error: Cannot construct instance of `com.managem.model.Region` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('4'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.managem.model.Region` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('4') at [Source: (PushbackInputStream); line: 5, column: 13] (through reference chain: com.managem.model.Pays["region"])]
JSON:
{
"pays" : "USA",
"description" : "free country",
"etat" : "active",
"region_id" : 1
}