Section.class でカスケード保存を行うことは可能ですか? Section オブジェクトを作成し、ID なしで新しい質問を追加します。保存しようとすると、エラーが発生します:
org.postgresql.util.PSQLException: エラー: テーブル "question_to_section" の挿入または更新が外部キー制約 "fk_2br9f09ok965403a9rv5y2n10" に違反しています 詳細: キー (question_id)=(0) がテーブル "question" に存在しません。
@Embedded アノテーションも使用しようとしましたが、失敗しました。
セクション クラス:
@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);
...
}
セクションクラスへの質問
@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;
...
}
質問セクション ID
public class QuestionSectionId implements Serializable {
long questionId;
long sectionId;
}