title: Doctrine won't save one-to-many relationship
body: <p>I'm being bugged by an issue that seems very very puzzling. FYI - I know and I have read most of the doctrine questions around here, so I know the basics of doctrine and specifying relationships.</p>
  
  <p>Below is how my data model looks (posting relevant sections of code)</p>
  
  <pre><code>class Sample
  {
  
      /**
       * @ORM\OneToMany(targetEntity="Analysis", mappedBy="sample", cascade={"persist"})
       * 
       protected $analyses
  
       public function addAnalysis(Analysis $analysis)
       {
          $analyses-&gt;setSample($this);
          $this-&gt;analyses[] = $analyses;
       }
  }
  </code></pre>
  
  <p>And Analysis</p>
  
  <pre><code>class Analysis
  {
  
      /**
       * @ORM\ManyToOne(targetEntity="Sample", inverseBy="analyses", cascade={"persist"})
       * @ORM\JoinColumn(name="sample_id", referencedColumnName="id")
       * 
       protected $sample
  
       public function setSample(Sample $sample)
       {
          $this-&gt;sample = $sample;
       }
  }
  </code></pre>
  
  <p>So one <code>Sample</code> can have multiple <code>Analysis</code>. While creating a new <code>Analysis</code> however, it is not letting me create one. It is throwing a NOT NULL constraint exception.</p>
  
  <p>Below is the code I tried.</p>
  
  <pre><code>$analysis = new Analysis
  $analysis-&gt;setUUID("seeebbbfg");
  $analysis-&gt;setStatus(Analysis::STATUS_DONE);
  $sample = $sample-&gt;addAnalysis($analysis)
  $em-&gt;persist($sample);
  $em-&gt;flush();
  </code></pre>
  
  <p>I have gone through many links and the doctrine documentation</p>
  
  <p><a href="https://stackoverflow.com/questions/22123521/doctrine-one-to-">Doctrine One-To-Many Relationship Won&#39;t Save - Integrity Constraint Violation</a>
  many-relationship-wont-save-integrity-constraint-violation
  <a href="https://stackoverflow.com/questions/26578942/symfony-2-doctrine-persist-doesnt-work-after-updating-relationship-mapping">Symfony 2 doctrine persist doesn&#39;t work after updating Relationship Mapping</a>
  <a href="https://stackoverflow.com/questions/18171801/doctrine-entities-relationship">Doctrine entities relationship</a></p>
  
  <p>After going through this <a href="https://stackoverflow.com/questions/30471789/doctrine-a-new-entity-was-found-through-the-relationship-error">Doctrine &quot;A new entity was found through the relationship&quot; error</a>, I tried to persist <code>$analysis</code> before persisting sample, but it gave an 'a new entity was found' error and then this official doctrine documentation
  <a href="http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html" rel="noreferrer">http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html</a></p>
  
  <p>Not sure what I'm missing. Can anyone shed any light on this? </p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <pre><code>[Doctrine\DBAL\Exception\NotNullConstraintViolationException]
  
  An exception occurred while executing
  
  INSERT INTO analysis (id, uuid, type, status, submission_at, sample_id) VALUES (?,?,?,?,?,?) with params [202066, "seeebbbfg", "temp", 2, "2016-5-22 12:16:39", null]
  null value in column "sample_id" violates not-null constraint
  </code></pre>