Given your setup and the JSON you're working with, it seems the problem arises from the way you're managing the client and service references prior to saving the Turno
. JPA won't automatically handle those relationships just based on IDs in nested objects. When you only include the IDs (like { "id": 1 }
) in your request, Spring can't figure out how to convert these into managed entities unless you do it yourself. The fix? Before you save the Turno
, retrieve the Cliente
and Servicio
from the database and link them to the Turno
. For example:
Cliente cliente = clienteRepository.findById(turno.getCliente().getId()).orElseThrow();
Servicio servicio = servicioRepository.findById(turno.getServicio().getId()).orElseThrow();
turno.setCliente(cliente);
turno.setServicio(servicio);
turnoRepository.save(turno);
This makes sure that Turno
points to the right entities, and JPA will handle the foreign keys without a hitch.