我正在使用Hibernate,我有这两个类。
public class ApplicantYear{
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "APPLICANT_ID")
private Applicant applicant;
.
.
.
}
public class Applicant {
@OneToMany(mappedBy = "applicant", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.REFRESH})
private Set<ApplicantYear> applicantYears = new HashSet<ApplicantYear>();
.
.
.
}
Public void testPersistence(){
Applicant appl= new ....
ApplicantYear applYear= new(appl, ...)
//我应该使用appl.persist()还是applYear.persist(),以便双方一次性保留?
}
答案 0 :(得分:1)
如API中所述,操作级联到关联实体。在你的情况下,这意味着调用
entityManager.persist(instanceOfApplicant)
将级联持久化操作也转移到applicantYears
列表的元素。刷新操作的功能类似。