title: Getting "Integrity constraint violation: 1048 Column 'payment_id' cannot be null" using Doctrine & Symfony
body: <p>I've been stuck for a couple of days dealing with this issue. I've been reviewing other StackOverflow questions and different forums but I couldn't get this to work, so that's the reason of this question.</p>
  
  <p>I'm developing a system that contains payments, so I've created a "Payment" class as follow:</p>
  
  <pre><code>/**
   * Payment
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="PaymentRepository")
   */
   class Payment
   {
  
     /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       * @JMS\Groups({"public"})
       * @JMS\Type("integer")
       */
       protected $id;
  
     /**
       * @var ArrayCollection
       *
       * @ORM\OneToMany(targetEntity="PaymentLine", mappedBy="payment", cascade={"persist",    "remove"})
       * @Assert\Valid()
       * @JMS\Groups({"public","create"})
       * @JMS\Type("ArrayCollection&lt;JensenTech\PaymentBundle\Entity\PaymentLine&gt;")
       */
       protected $paymentLines;
  
       /**
         * @var string
         *
         * @ORM\Column(name="total_net", type="decimal", precision=5, scale=2)
         * @Assert\NotBlank(message="Invalid Net Amount")
         * @JMS\Groups({"public","create"})
         */
         protected $totalNet;
  
       /**
         * @var string
         * @ORM\Column(name="total_vat", type="decimal", precision=5, scale=2)
         * @Assert\NotBlank(message="Invalid VAT Amount")
         * @JMS\Groups({"public","create"})
         */
         protected $totalVat;
  
        /**
          * @var string
          * @ORM\Column(name="total_gross", type="decimal", precision=5, scale=2)
          * @Assert\NotBlank(message="Invalid Gross Amount")
          * @JMS\Groups({"public","create"})
          */
          protected $totalGross;
  
  }
  </code></pre>
  
  <p>And I've created another class called PaymentLine to store details about each payment line:</p>
  
  <pre><code>/**
    * PaymentLine
    *
    * @ORM\Table()
    * @ORM\Entity
    * @ORM\HasLifecycleCallbacks()
    */
    class PaymentLine
    {
  
     /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       * @JMS\Exclude()
       */
       protected $id;
  
     /**
       * @var Payment
       * @ORM\ManyToOne(targetEntity="Payment", inversedBy="paymentLines")
       * @ORM\JoinColumn(nullable=false)
       * @JMS\Exclude()
       */
       protected $payment;
  
     /**
       * @var string
       *
       * @ORM\Column(name="concept", type="string", length=255)
       * @JMS\Groups({"public", "create"})
       */
       protected $concept;
  
     /**
       * @var integer
       *
       * @ORM\Column(name="quantity", type="smallint")
       * @JMS\Groups({"public", "create"})
       */
       protected $quantity;
  
     /**
       * @var float
       *
       * @ORM\Column(name="unit_price", type="decimal", precision=5, scale=2)
       * @JMS\Groups({"public", "create"})
       */
      protected $unitPrice;
  }
  </code></pre>
  
  <p>As you can see this is a OneToMany association, this association is handled by form to validate the data. After the data is validate, I want to store it in the database to process it after, so I use these code lines to do that:</p>
  
  <pre><code> $payment = new Payment();
   $paymentForm = $this-&gt;createForm('payment', $payment);
  
   $paymentForm-&gt;handleRequest($request);
  
   if ($paymentForm-&gt;isValid()) {            
  
      $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
      $entityManager-&gt;persist($payment);
      $entityManager-&gt;flush();
   }
  </code></pre>
  
  <p>This code handle the form that receives all the payment data (payment data and payment lines data) to validate them and store in the database. When I execute this code, I get this error:</p>
  
  <p>SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'payment_id' cannot be null</p>
  
  <p>Every advice will be welcome and appreciated.</p>
  
  <p>Thank you in advance.</p>