在一对多关系的哪一方,我应该坚持持久的方法来坚持双方

时间:2012-05-29 17:19:55

标签: hibernate jpa persistence

我正在使用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(),以便双方一次性保留?

}

1 个答案:

答案 0 :(得分:1)

API中所述,操作级联到关联实体。在你的情况下,这意味着调用

entityManager.persist(instanceOfApplicant)

将级联持久化操作也转移到applicantYears列表的元素。刷新操作的功能类似。