Is it possible to do a cascade save in Section.class? I create Section object and add new questions without id. When I try save it I get error:
org.postgresql.util.PSQLException: ERROR: insert or update on table "question_to_section" violates foreign key constraint "fk_2br9f09ok965403a9rv5y2n10" Details: Key (question_id)=(0) is not present in table "question".
I also tried to use @Embedded annotation, but unsuccessfully.
Section class:
@Table(name = "section")
@Entity(name = "section")
public class Section implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
@JsonProperty
long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "section")
Set<QuestionToSection> questions = new HashSet<QuestionToSection>(0);
...
}
Question To Section class
@Entity(name = "question_to_section")
@Table(name = "question_to_section")
@IdClass(QuestionSectionId.class)
public class QuestionToSection implements Serializable {
@Id
long sectionId;
@Id
long questionId;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "sectionId", nullable = false, updatable = false, insertable = false, referencedColumnName = "id")
Section section;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL )
@JoinColumn(name = "questionId", nullable = false, updatable = false, insertable = false, referencedColumnName = "id")
Question question;
...
}
QuestionSectionId
public class QuestionSectionId implements Serializable {
long questionId;
long sectionId;
}