简体   繁体   English

Spring Data JPA一对多关系不保存子表

[英]Spring Data JPA One to Many relationship not saving child table

Parent Entity 父实体

@OneToMany(fetch = FetchType.EAGER, mappedBy = "paramRef", cascade=CascadeType.ALL)
public Set getParamList() { return this.paramList; }

Child Entity 儿童实体

@ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "param_Ref_Id", nullable = false, insertable=false, updatable=false)
public ParamRef1 getParamRef() { return this.paramRef; }

Code to persist 代码坚持下去

ParamRef1 pr = new ParamRef1();
pr.setName("TEST PARAM");

Param1 p1 = new Param1() p1.setParamValue("ONE") p1.setParamRef(pr);
Param1 p2 = new Param1() p2.setParamValue("TWO") p2.setParamRef(pr);

Set paramList = new HashSet() paramList.add(p1) paramList.add(p2)

pr.setParamList(paramList)

pr = paramRefDao1.save(pr)

Please let me know if the steps are correct. 如果步骤正确,请告诉我。 I am getting following exception. 我得到以下异常。 and not able to understand why parent id is not available in child table 并且无法理解为什么父ID在子表中不可用

Caused by: org.hibernate.exception.ConstraintViolationException: Cannot insert the value NULL into column 'PARAM_REF_ID', table 'PARAM'; 引起:org.hibernate.exception.ConstraintViolationException:无法将值NULL插入到'PARAM_REF_ID'列中,表'PARAM'; column does not allow nulls. 列不允许空值。 INSERT fails. INSERT失败。

You are missing value for the column PARAM_REF_ID for the parent table 您缺少父表的列PARAM_REF_ID

ParamRef1 pr = new ParamRef1();
pr.setName("TEST PARAM");
pr.setParamRefId(1); // replace 1 with any other valid id as value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM