I'm having a problem implementing a bi-directional parent/child relationship using hibernate 3. The parent, in this case is of the class ReportCriteria. The child is of class PkVisit. I've pasted my hibernate configuration files as well as the underlying java objects below.
ReportCriteria configuration:
<hibernate-mapping package="org.fstrf.masterpk.domain">
<class name="ReportCriteriaBean" table="masterPkReportCriteria">
<id name="id" column="id">
<generator class="org.hibernate.id.IncrementGenerator" />
</id>
<bag name="pkVisits" table="masterPkWeeks" cascade="all-delete-orphan" inverse="true">
<key column="runId"/>
<one-to-many class="PkVisit"/>
</bag>
</class>
</hibernate-mapping>
ReportCriteria bean:
public class ReportCriteriaBean {
private Integer id;
private List<PkVisit> pkVisits = LazyList.decorate(new ArrayList(), FactoryUtils.instantiateFactory(PkVisit.class));
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<PkVisit> getPkVisits() {
return pkVisits;
}
public void setPkVisits(List<PkVisit> pkVisits) {
this.pkVisits = pkVisits;
}
}
PkVisit Configuration:
<hibernate-mapping package="org.fstrf.masterpk.domain">
<class name="PkVisit" table="masterPkWeeks">
<id name="id" column="id">
<generator class="org.hibernate.id.IncrementGenerator" />
</id>
<many-to-one name="reportCriteriaBean" class="ReportCriteriaBean" column="runid" not-null="true" />
<property name="week" column="week" />
</class>
</hibernate-mapping>
PkVisit Bean:
public class PkVisit {
private Integer id;
private ReportCriteriaBean reportCriteriaBean;
private Integer week;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public ReportCriteriaBean getReportCriteriaBean() {
return reportCriteriaBean;
}
public void setReportCriteriaBean(ReportCriteriaBean reportCriteriaBean) {
this.reportCriteriaBean = reportCriteriaBean;
}
public Integer getWeek() {
return week;
}
public void setWeek(Integer week) {
this.week = week;
}
}
The problem occurs when I try to save an instance of ReportCriteria, which, due to the cascade should also save any child PkVisits as well. However, when the save is called using
hibernateTemplate.saveOrUpdate(reportCriteria);
The following error is generated:
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: org.fstrf.masterpk.domain.PkVisit.reportCriteriaBean; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: org.fstrf.masterpk.domain.PkVisit.reportCriteriaBean
When I save a report criteria that contains no PkVisits then everything works as I would expect, but as soon as any elements are in the pkVisits list of the ReportCriteria bean the errors occurs.
SOLUTION EDIT:
My problem was that I was never explicitly setting the parent (ReportCriteriaBean) in the children (PkVisits). I remedied the problem by editing my PkVisits setter in the following way:
public void setPkVisits(List<PkVisit> pkVisits) {
this.pkVisits = pkVisits;
for(PkVisit visit : pkVisits){
visit.setReportCriteriaBean(this);
}
}