Estou tentando salvar 3 objetos relacionados no banco de dados, mas não faço ideia do porquê não funciona.
Log:
2023-01-27T01:15:08.960-03:00 INFO 8532 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-01-27T01:15:08.960-03:00 INFO 8532 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-01-27T01:15:08.961-03:00 INFO 8532 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
newTso executado em 0Ms
Iniciando novoTso
toTso chamado
toTso concluido
2023-01-27T01:15:33.190-03:00 WARN 8532 --- [nio-8080-exec-5] o.h.a.i.UnresolvedEntityInsertActions : HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
Unsaved transient entity: ([br.com.fanthom.boot.model.Cliente#<null>])
Dependent entities: ([[br.com.fanthom.boot.model.Tso#<null>]])
Non-nullable association(s): ([br.com.fanthom.boot.model.Tso.cliente])
2023-01-27T01:15:33.193-03:00 ERROR 8532 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : br.com.fanthom.boot.model.Tso.cliente -> br.com.fanthom.boot.model.Cliente] with root cause
--
package br.com.fanthom.boot.model;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "tsos")
public class Tso {
@jakarta.persistence.Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
private BigDecimal ValorTotal;
@ManyToOne
@JoinColumn(name = "Cliente_id", nullable = false)
private Cliente cliente;
private LocalDate dataDaCria;
private LocalDate dataDaEntrega;
@ManyToMany
private List<Produto> produtos;
public Tso() {
}
//getters e setters omitidos...
--
package br.com.fanthom.boot.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "Clientes")
public class Cliente {
@jakarta.persistence.Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
private String nome;
private String tel;
private String cpf;
private String email;
@OneToOne
@JoinColumn(name = "Endereco_id", nullable = false)
Endereco endereco;
public Cliente() {
}
//mais get e set...
--
package br.com.fanthom.boot.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "Enderecos")
public class Endereco {
@jakarta.persistence.Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
private String cep;
private String rua;
private String numero;
private String bairro;
private String cidade;
private String comp;
@OneToOne
private Cliente cliente;
public Endereco() {
}
--
package br.com.fanthom.boot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import br.com.fanthom.boot.dto.RequestNovoTso;
import br.com.fanthom.boot.model.Tso;
import br.com.fanthom.boot.repository.TsoRepository;
@Controller
@RequestMapping("tso")
public class TsoController {
@Autowired
private TsoRepository tsoRepository;
@RequestMapping(method = RequestMethod.POST, value = "novo")
public String novo(RequestNovoTso request) {
System.out.println("Iniciando novoTso");
Tso tso = request.toTso();
tsoRepository.save(tso);
System.out.println(" novoTso executado :)");
return "tso/formulario";
}