I have two tables :
societe (
id SERIAL PRIMARY KEY,
...,
fk_adresse int NOT NULL
);
(fk_adresse is a foreign key)
The other table is
bureau (
fk_societe INT PRIMARY KEY NOT NULL,
...,
fk_convention INT NOT NULL,
....
);
fk_societe is both the primary key of the "bureau" table and a foreign key towards the "societe" table and fk_convention is a foreign key
My Java mapping for bureau is :
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "fk_societe", nullable = false)
private Integer fkSociete;
@OneToOne(fetch = FetchType.LAZY, optional = true, cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumn(name = "fk_societe", referencedColumnName = "id") })
private Societe societe;
....
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "fk_convention", referencedColumnName = "id")
private Convention convention;
....
My controller is
@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded;application/json")
public void createBureau(Bureau bureau){
bureauRepository.save(bureau);
}
@GetMapping("/bureaux")
public List<Bureau> getAllBureaux() {
return bureauRepository.findAll();
}
//recherche de bureau
@GetMapping("/{id}")
public ResponseEntity<Bureau> getBureauById(@PathVariable(value = "id") Integer id)
throws ResourceNotFoundException {
Bureau bureau = bureauRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Bureau not found");
return ResponseEntity.ok().body(bureau);
}
The search of one bureau and the list of all the bureau work fine but the creation of one bureau does not work. In my societe table I have a societe with the id 42 so I send a create (POST) request with Postman and with the following data :
fk_societe 42
fk_devise 1
fk_langue 67
fk_convention 2
est_4_e_directive true
est_transitoire false
Postman says to me that there is a 500 error and in my console I have the following error
2020-10-16 11:50:07.115 ERROR 8368 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne ½ fk_societe ╗ dans la relation ½ bureau ╗
DÚtailá: La ligne en Úchec contient (null, 0, 0, null, f, f).
which is translated by
ERROR: a NULL value violates the NOT NULL constraint of the fk_societe column in the bureau relationship
Details : the failing line contains (null, 0, 0, null, f, f).
Maybe my foreign keys are not correctly mapped...