0

我正在使用 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() 以便双方都被持久化//一次?

}
4

1 回答 1

1

正如在API中所说的,操作被级联到关联的实体。在您的情况下,这意味着调用

entityManager.persist(instanceOfApplicant)

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

于 2012-05-29T18:33:14.970 回答