title: Doctrine 2 link table relationship - integrity constraint violation
body: <p>I have a scenario, where i am linking the following tables:</p>
  
  <p>organization
  employee
  org_employees</p>
  
  <p>So i link the entoties in the following manner.</p>
  
  <p>Employee - OneToMany - OrgEmployee(link table) - ManyToOne</p>
  
  <p>But i am getting the following error: </p>
  
  <pre><code>An exception occurred while executing 'INSERT INTO org_branch_employees (is_active, created_at, created_by, modified_at, modified_by, employee_id, org_branch_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params [\"1\", \"2016-02-04 20:03:22\", 1, \"2016-02-04 20:03:22\", 1, null, null]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'employee_id' cannot be null"
  </code></pre>
  
  <p>I am frustrated, where i am wrong, can anyone help me out.</p>
  
  <p>Employee Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace Employee\Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  use Library\Entity\BaseEntity;
  
  use Users\Entity\User;
  use Organization\Entity\Organization;
  //use Organization\Entity\OrgEmployee;
  
  /**
   * Description of Employee
   *
   * @author Macwin
   */
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="employees")
   */
  
  class Employee extends BaseEntity{
  
      /**
       * @ORM\OneToOne(
       *     targetEntity="Users\Entity\User"
       * )
       * @ORM\JoinColumn(
       *     name="user_id",
       *     referencedColumnName="id",
       *     nullable=false
       * )
       */
      private $user;
  
      /**
       * @ORM\Column(name="employee_code", type="string")
       * @var string
       */
      protected $empCode;
  
      /**
       * @ORM\OneToOne(
       *     targetEntity="Organization\Entity\Organization"
       * )
       * @ORM\JoinColumn(
       *     name="org_id",
       *     referencedColumnName="id",
       *     nullable=false
       * )
       */
      private $organization;
  
      /**
       * @ORM\OneToMany(targetEntity="Employee\Entity\OrgEmployee", mappedBy="employee")
       */
      protected $orgEmployee;
  
      public function __construct() {
  
          $this-&gt;organizations = new \Doctrine\Common\Collections\ArrayCollection();
  
      }
  
      public function getOrganizations()
      {
          return $this-&gt;organizations;
      }
  
      public function addOrganization(Organization $organization = null)
      {
          $this-&gt;organizations-&gt;add($organization);
      }
  
      public function setUser(User $user = null)
      {
          $this-&gt;user = $user;
  
          return $this;
      }
  
      public function getUser()
      {
          return $this-&gt;user;
      }
  
      public function getEmpCode() {
          return $this-&gt;empCode;
      }
  
      public function setEmpCode($empCode) {
          $this-&gt;empCode = $empCode;
          return $this;
      }
  
      public function setOrganization(Organization $organization = null)
      {
          $this-&gt;organization = $organization;
  
          return $this;
      }
  
      public function getOrganization()
      {
          return $this-&gt;organization;
      }
  
      public function __toString() {
          return __CLASS__ . ": [id: {$this-&gt;id}, name: {$this-&gt;name}]";
      }
  }
  </code></pre>
  
  <p>Here is my Organization Entity:</p>
  
  <pre><code> &lt;?php
  
      namespace Organization\Entity;
  
      use Doctrine\Common\Collections\ArrayCollection;
      use Doctrine\ORM\Mapping as ORM;
      use Library\Entity\BaseEntity;
  
      use Organization\Entity\OrganizationType;
      use Organization\Entity\OrgEmployee;
  
  
      /**
       * Description of Organization
       *
       * @author Macwin
       */
  
      /**
       * @ORM\Entity
       * @ORM\Table(name="organizations")
       */
  
      class Organization extends BaseEntity{
  
  
  
          /**
           * @ORM\Column(name="name", type="string")
           * @var string
           */
          protected $name;
  
          /**
           * @ORM\OneToOne(
           *     targetEntity="Organization\Entity\OrganizationType"
           * )
           * @ORM\JoinColumn(
           *     name="org_type_id",
           *     referencedColumnName="id",
           *     nullable=false
           * )
           */
          private $orgType;
  
          /**
           * 
           * @ORM\OneToOne(targetEntity="Organization\Entity\Organization")
           * @ORM\JoinColumn(name="parent_org_id", referencedColumnName="id")
           */
          protected $parent;
  
          /**
           * @ORM\Column(name="description", type="string")
           * @var string
           */
          protected $description;
  
          /**
           * @var string
           *
           * @ORM\Column(name="address_line1", type="string", length=255, nullable=true)
           */
          private $addressLine1;
  
          /**
           * @var string
           *
           * @ORM\Column(name="address_line2", type="string", length=255, nullable=true)
           */
          private $addressLine2;
  
          /**
           * @var string
           *
           * @ORM\Column(name="zipcode", type="string", length=255, nullable=true)
           */
          private $zipcode;
  
          /**
           * @ORM\OneToOne(
           *     targetEntity="Library\Entity\Country"
           * )
           * @ORM\JoinColumn(
           *     name="country",
           *     referencedColumnName="id",
           *     nullable=false
           * )
           */
          private $country;
  
          /**
           * @ORM\OneToOne(
           *     targetEntity="Library\Entity\State"
           * )
           * @ORM\JoinColumn(
           *     name="state",
           *     referencedColumnName="id",
           *     nullable=false
           * )
           */
          private $state;
  
  
          /**
           * @ORM\OneToOne(
           *     targetEntity="Library\Entity\City"
           * )
           * @ORM\JoinColumn(
           *     name="city",
           *     referencedColumnName="id",
           *     nullable=false
           * )
           */
          private $city;
  
  
          /**
           * @ORM\OneToOne(
           *     targetEntity="Library\Entity\Area"
           * )
           * @ORM\JoinColumn(
           *     name="area",
           *     referencedColumnName="id",
           *     nullable=false
           * )
           */
          private $area;
  
  
          /**
           * @ORM\OneToMany(targetEntity="Employee\Entity\OrgEmployee", mappedBy="organization")
           */
          protected $orgEmployee;
  
          public function getName() {
              return $this-&gt;name;
          }
  
          public function setName($name) {
              $this-&gt;name = $name;
          }
  
          public function setOrgType(OrganizationType $orgType = null)
          {
              $this-&gt;orgType = $orgType;
  
              return $this;
          }
  
          public function getOrgType()
          {
              return $this-&gt;orgType;
          }
  
          public function getParent() {
              return $this-&gt;parent;
          }
  
          public function setParent($parent) {
              $this-&gt;parent = $parent;
          }
  
          public function getDescription() {
              return $this-&gt;description;
          }
  
          public function __toString() {
              return __CLASS__ . ": [id: {$this-&gt;id}, name: {$this-&gt;name}]";
          }
      }
  </code></pre>
  
  <p>Here is the OrgEmployee Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace Employee\Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  use Library\Entity\BaseEntity;
  use Employee\Entity\Employee;
  use Organization\Entity\Organization;
  
  /**
   * Description of Org Employees
   *
   * @author Macwin
   */
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="org_branch_employees")
   */
  
  class OrgEmployee extends BaseEntity{
  
      /**
       * @ORM\ManyToOne(targetEntity="Employee\Entity\Employee", inversedBy="orgEmployee")
       * @ORM\JoinColumn(name="employee_id",referencedColumnName="id",nullable=false)
       */
      protected $employee;
  
      /**
       * @ORM\ManyToOne(targetEntity="Organization\Entity\Organization", inversedBy="orgEmployee")
       * @ORM\JoinColumn(name="org_branch_id", referencedColumnName="id", nullable=false)
       */
      protected $organization;
  
      public function setEmployees(Employee $employee = null)
      {
          $this-&gt;employees = $employee;
  
          return $this;
      }
  
      public function getEmployees()
      {
          return $this-&gt;employees;
      } 
  
      public function setOrganizations(Organization $organization = null)
      {
          $this-&gt;organizations = $organization;
  
          return $this;
      }
  
      public function getOrganizations()
      {
          return $this-&gt;organizations;
      }    
  
  
  }
  </code></pre>