是否可以在 Section.class 中进行级联保存?我创建 Section 对象并添加没有 id 的新问题。当我尝试保存它时,我收到错误:
org.postgresql.util.PSQLException:错误:在表“question_to_section”上插入或更新违反了外键约束“fk_2br9f09ok965403a9rv5y2n10”详细信息:表“问题”中不存在键(question_id)=(0)。
我也尝试使用@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;
...
}
QuestionSectionId
public class QuestionSectionId implements Serializable {
long questionId;
long sectionId;
}