Olá estou escrevendo uma API Rest e criei duas entidades, Usuario e Curso. Criei uma relação bidirecional entre as duas sendo a Usuario @OneToMany e a curso @ManyToOne. Até ai tudo bem e funcionando, acontece que se eu tento passar um POST para o endpoint cursos com um usuario inexistente ele me retorna:
2021-02-22 20:50:51.906 ERROR 12768 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
Eu acredito que deva retornar um status 400 de BAD REQUEST, como fizemos no curso, mas essa exceção que recebi é bem genérica, não da pra usar a MethodArgumentNotValidException. ela só me informa que o campo usuario_id que deveria ter algum valor, está NULL por que o usuário não existe.
Agradeço por qualquer ajuda!