Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 在一对多关系的哪一侧,我应该调用persist方法来持久化双方_Hibernate_Jpa_Persistence - Fatal编程技术网

Hibernate 在一对多关系的哪一侧,我应该调用persist方法来持久化双方

Hibernate 在一对多关系的哪一侧,我应该调用persist方法来持久化双方,hibernate,jpa,persistence,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, cascad

我正在使用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, ...)
例如,如中所述,操作被级联到相关实体。在你的情况下,这意味着

entityManager.persist(instanceOfApplicant)

还将对
applicationears
列表的元素执行级联持久化操作。刷新操作的功能类似

谢谢你@Mikko。反过来行吗?i、 e如果我使用entityManager.persist(instanceofapplicationyear)取决于您对“工作”的理解。当然,它不会级联持久化,所以如果instanceOfApplicantYear有申请人引用的新实例,它将不起作用。
entityManager.persist(instanceOfApplicant)