user3560205
user3560205

Reputation: 101

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

Caused by: org.hibernate.exception.ConstraintViolationException: Cannot insert the value NULL into column 'PARAM_REF_ID', table 'PARAM'; column does not allow nulls. INSERT fails.

Upvotes: 3

Views: 7084

Answers (1)

senseiwu
senseiwu

Reputation: 5269

You are missing value for the column PARAM_REF_ID for the parent table

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

Upvotes: 1

Related Questions