title: Hibernate bidirectional parent/child problem
body: <p>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.</p>
  
  <p>ReportCriteria configuration:</p>
  
  <pre><code>&lt;hibernate-mapping package="org.fstrf.masterpk.domain"&gt;
  &lt;class name="ReportCriteriaBean" table="masterPkReportCriteria"&gt;
  
      &lt;id name="id" column="id"&gt;
          &lt;generator class="org.hibernate.id.IncrementGenerator" /&gt;
      &lt;/id&gt;
  
      &lt;bag name="pkVisits" table="masterPkWeeks" cascade="all-delete-orphan" inverse="true"&gt;
          &lt;key column="runId"/&gt;
          &lt;one-to-many class="PkVisit"/&gt;
      &lt;/bag&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>ReportCriteria bean:  </p>
  
  <pre><code>public class ReportCriteriaBean {
  
  private Integer id;
  
  private List&lt;PkVisit&gt; pkVisits =  LazyList.decorate(new ArrayList(), FactoryUtils.instantiateFactory(PkVisit.class));
  
  public Integer getId() {
      return id;
  }
  public void setId(Integer id) {
      this.id = id;
  }
  
  public List&lt;PkVisit&gt; getPkVisits() {
      return pkVisits;
  }
  
  public void setPkVisits(List&lt;PkVisit&gt; pkVisits) {
      this.pkVisits = pkVisits;
  }
  
  }
  </code></pre>
  
  <p>PkVisit Configuration:</p>
  
  <pre><code>&lt;hibernate-mapping package="org.fstrf.masterpk.domain"&gt;
  &lt;class name="PkVisit" table="masterPkWeeks"&gt;
      &lt;id name="id" column="id"&gt;
          &lt;generator class="org.hibernate.id.IncrementGenerator" /&gt;
      &lt;/id&gt;
  
      &lt;many-to-one name="reportCriteriaBean" class="ReportCriteriaBean" column="runid" not-null="true" /&gt;
  
      &lt;property name="week" column="week" /&gt;
  
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>PkVisit Bean:</p>
  
  <pre><code>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;
  }
  }
  </code></pre>
  
  <p>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</p>
  
  <pre><code>hibernateTemplate.saveOrUpdate(reportCriteria);
  </code></pre>
  
  <p>The following error is generated:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>SOLUTION EDIT:</p>
  
  <p>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:</p>
  
  <pre><code>public void setPkVisits(List&lt;PkVisit&gt; pkVisits) {
      this.pkVisits = pkVisits;
      for(PkVisit visit : pkVisits){
          visit.setReportCriteriaBean(this);
      }
  }
  </code></pre>