title: Doctrine one-to-one unidirectional
body: <p>In Symfony 3 with Doctrine I'm trying to get a one-to-one unidirectional relationship with both tables sharing the same primary key working. To do so I'm trying to replicate the example on the <a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html" rel="nofollow noreferrer">Doctrine Association Mapping</a> page. </p>
  
  <p>However, the one-to-one uni documentation has no examples of setters and getters - and there is no definition of the id field on the target entity, either. So I tried to experiment around myself.</p>
  
  <p>These are my entities:</p>
  
  <pre><code>class Country
  {
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @ORM\OneToOne(targetEntity="MySubEntity", cascade={"persist", "remove"})
       * @ORM\JoinColumn(name="id", referencedColumnName="id", nullable=true)
       */
      private $mysubentity;
      [...]
      /**
       * @return MySubEntity
       */
      public function getMySubEntity()
      {
          return $this-&gt;mysubentity;
      }
  
      /**
       * @param MySubEntity $mysubentity
       */
      public function setMySubEntity($mysubentity)
      {
          $this-&gt;mysubentity = $mysubentity;
      }
  }
  
  class MySubEntity
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      [..]
  
      /**
       * Set id
       *
       * @param $id
       *
       * @return MySubEntity
       */
      public function setId($id)
      {
          $this-&gt;id = $id;
  
          return $this;
      }
  
      /**
       * Get id
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  }
  </code></pre>
  
  <p>When I persist the country entity, I receive <code>Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails</code>. When inspecting the data I can see that Doctrine attempted to set the id of MySubEntity to 0.</p>
  
  <p>Does anybody have an idea what I need to do for the MySubEntity $id field to be auto-populated from the Country entity?</p>