I’ve done some API in spring and try to add some data in the server with angular.
Here’s the @Entity in Spring
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
long id;
@Column(name="nome")
String name;
@Column(name = "descrizione")
String descrizione;
@Column(name = "img")
String img;
@ManyToOne
@JoinColumn (name = "id_luogo")
Luoghi idLuogo;
this is the method in the controller of the API call that I’m having issues with
@PostMapping("/add")
public ResponseEntity<IncontriTerreni> addIncontro(@RequestBody IncontriTerreni incontro){
IncontriTerreni newIncontro= service.addIncontro(incontro);
return new ResponseEntity<>(newIncontro,HttpStatus.CREATED);
}
In angular I’ve created a dialog that pass some data, the id is present in the form but when I click the submit button it gaves me an internal server error
add-encounter-form.component.ts:31
POST http://localhost:9595/oota-reg/api/incontri-terreni/add 500
and in the error it says:
“could not execute statement [Column ‘id_luogo’ cannot be null] [insert into incontri_terreni (descrizione,id_luogo,img,nome) values (?,?,?,?)]; SQL [insert into incontri_terreni (descrizione,id_luogo,img,nome) values (?,?,?,?)]; constraint [null]”
How can I solve this?
This is the form :
idLuogo
Nome
Descrizione
Link Immagine
<div>
<button mat-stroked-button color="primary" type="submit">Invia</button>
</div>
</form>
And this is the TS file
export class AddEncounterFormComponent implements OnInit{
ngOnInit(): void {
this.incontro.id_luogo.id = this.data.id
}
private service :TerreniService = inject(TerreniService);
public data : any = inject(MAT_DIALOG_DATA);
onSubmit() {
console.log(this.incontro);
this.service.addIncontro(this.incontro).subscribe({
next: (resp) =>{
console.log(resp);
},
error: (err)=>{
console.log(err);
},
})
}
incontro : any={
name : "",
descrizione : "",
img : "",
id_luogo: {id: 0}
}
}