Olá pessoal, estou tendo um problema com persistência e salvamento de transiente no JPA do SpringBoot.
Tenho duas classes, Time e Jogador, sendo que além de um atributo que é a lista de jogadores do time, tenho também na classe Time um atributo Jogador que é o capitão.
Não conheço muito ainda sobre anotações mas pelo pouco entendi pode ser feito da seguinte forma:
package com.samuelfranck.campeonatohandebol.domain;
@Entity
public class Jogador implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String nome;
private Date dataNascimento;
private String genero;
private Double altura;
private Double peso;
@ManyToOne
@JoinColumn(name="time_id")
private Time timeEmQueJoga;
public Jogador() {
}
public Jogador(Integer id, String nome, Date dataNascimento, String genero, Double altura, Double peso) {
super();
this.id = id;
this.nome = nome;
this.dataNascimento = dataNascimento;
this.genero = genero;
this.altura = altura;
this.peso = peso;
}
“+ getters, setters e hashcode equals”
}
package com.samuelfranck.campeonatohandebol.domain;
@Entity
public class Time implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String nome;
@OneToOne
@JoinColumn(name="capitao_id")
private Jogador capitao;
@OneToMany(mappedBy="timeEmQueJoga")
private List<Jogador> jogadores = new ArrayList<>();
public Time() {
}
public Time(Integer id, String nome) {
super();
this.id = id;
this.nome = nome;
}
" + getters, setters e hashcode equals"
Os erros que ocorrem sãos estes:
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.samuelfranck.campeonatohandebol.domain.Jogador.timeEmQueJoga -> com.samuelfranck.campeonatohandebol.domain.Time; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.samuelfranck.campeonatohandebol.domain.Jogador.timeEmQueJoga -> com.samuelfranck.campeonatohandebol.domain.Time
Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.samuelfranck.campeonatohandebol.domain.Jogador.timeEmQueJoga -> com.samuelfranck.campeonatohandebol.domain.Time
Agradeço desde já.