9
When trying to save the "Addresses" of "Client" JPA/Spring returns me the following error in "main"
Caused by: org.hibernate.Transientpropertyvalueexception: Object References an Unsaved Transient instance - save the Transient instance before Flushing : com.nelioalves.cursomc.domain.Endereco.cidade -> com.nelioalves.curswto.domain.City
The code is like this:
Cliente cli1 = new Cliente(null, "Maria Silva", "[email protected]", "36378912377", TipoCliente.PESSOAFISICA);
cli1.getTelefones().addAll(Arrays.asList("27363323", "93838393"));
Endereco e1 = new Endereco(null, "Rua Flores", "300", "Apto 303", "Jardim", "38220834", cli1, c1);
Endereco e2 = new Endereco(null, "Avenida Matos", "105", "Sala 800", "Centro", "38777012", cli1, c2);
cli1.getEnderecos().addAll(Arrays.asList(e1, e2));
enderecoRepository.saveAll(Arrays.asList(e1, e2));
clienteRepository.saveAll(Arrays.asList(cli1));
When removing the address and saving only the client it saves normally.
Here is the class Endereco
:
@Entity
public class Endereco {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String logradouro;
private String numero;
private String complemento;
private String bairro;
private String cep;
@ManyToOne
@JoinColumn(name="cliente_id")
private Cliente cliente;
@ManyToOne
@JoinColumn(name="cidade_id")
private Cidade cidade;
public Endereco() {
}
Here is the class Cliente
:
@Entity
public class Cliente implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String nome;
private String email;
private String cpfOuCnpj;
private Integer tipo;
@OneToMany(mappedBy="cliente")
private List<Endereco> enderecos = new ArrayList<>();
@ElementCollection
@CollectionTable(name="TELEFONE")
private Set<String> telefones = new HashSet<>();
public Cliente() {
}
Hello, put the code in the question and not the links.
– renanvm