Boa noite Galera!
Estou tendo problema ao salvar um Quiz.
object references an unsaved transient instance - save the transient instance before flushing : plataforma.cardioweb.model.Answer.alternative -> plataforma.cardioweb.model.Alternative
Já tentei colocar os cascade e outras coisas, mas não resolveu, não consegui entender oque ta errado.
Essa alternative que eu uso na Answer já existe no banco, só quero salvar Quiz e Answer, não faz sentido esse erro.
Tenho as seguintes classes, com os seguintes mapeamentos.
public class Quiz implements Serializable {
private static final long serialVersionUID = 9127541877030609013L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "QUIZ_ID")
private Long quizId;
@OneToOne
@JoinColumn(name = "COMPANY_ID")
private Company company;
@JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@Column(name = "INCLUSION_DATE")
private LocalDateTime inclusionDate;
@OneToMany(mappedBy = "quiz", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
private List<Answer> answers;
}
public class Answer implements Serializable {
private static final long serialVersionUID = 7119174725032674042L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ANSWER_ID")
private Long answerId;
@OneToOne
@JoinColumn(name = "QUESTION_ID")
private Question question;
@ManyToOne
@JoinColumn(name = "QUIZ_ID")
private Quiz quiz;
@OneToOne
@JoinColumn(name = "ALTERNATIVE_ID")
private Alternative alternative;
@Column(name = "OTHER_VALUE")
private String otherValue;
@Column(name = "OPTION_VALUE")
private Integer optionValue;
@JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@Column(name = "INCLUSION_DATE")
private LocalDateTime inclusionDate;
}
public class Alternative implements Serializable {
private static final long serialVersionUID = 8518695669261487046L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ALTERNATIVE_ID")
private Long alternativeId;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "OPTION_VALUE")
private Integer optionValue;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "QUESTION_ID")
private Question question;
@OneToOne(mappedBy = "alternative", cascade = CascadeType.REFRESH)
private Answer answer;
}