title: Doctrine2 - how to change owning side, how to set not null
body: <p>I have such relation:</p>
  
  <pre class="lang-php prettyprint-override"><code>class TaskSet
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
  
      /**
       * @var \Doctrine\Common\Collections\ArrayCollection $tasks
       *
       * @ORM\OneToMany (targetEntity="Task", mappedBy="taskset", cascade={"ALL"})
       */
      private $tasks;
  </code></pre>
  
  <p>and </p>
  
  <pre class="lang-php prettyprint-override"><code>class Task
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var TaskSet $taskset
       *
       * @ORM\ManyToOne(targetEntity="TaskSet", inversedBy="tasks")
       */
      private $taskset;
  </code></pre>
  
  <p>generated SQL looks like:</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE task (
      id INT AUTO_INCREMENT NOT NULL, 
      taskset_id INT DEFAULT NULL, 
      INDEX IDX_527EDB255D67FAA4 (taskset_id), PRIMARY KEY(id)
  ) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB;
  
  CREATE TABLE task_set (
      id INT AUTO_INCREMENT NOT NULL, 
      PRIMARY KEY(id)
  ) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB;   
  
  ALTER TABLE task ADD CONSTRAINT FK_527EDB255D67FAA4 FOREIGN KEY (taskset_id) REFERENCES task_set (id)
  </code></pre>
  
  <p>Since now everything is well, I get one(testset) to many(test) bidirectional relation.</p>
  
  <p>Unfortunately Doctrine2 chooses Task entity as owning side so this code is not persisting relation between entities:</p>
  
  <pre class="lang-php prettyprint-override"><code>$em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
  $taskset = new TaskSet();
  $taskset-&gt;setName('task 1');
  $taskset-&gt;setDescription('desc 1');
  
  $task =  new Task();
  $task-&gt;setName('task 1');
  $task-&gt;setClassName('class name 1');
  $taskset-&gt;addTask($task);
  
  $em-&gt;persist($taskset);
  $em-&gt;flush();
  </code></pre>
  
  <p>output from code above is:</p>
  
  <pre class="lang-sql prettyprint-override"><code>INSERT INTO `task_set` (`id`, `name`, `description`) VALUES (1, 'task 1', 'desc 1');
  INSERT INTO `task` (`id`, `taskset_id`, `name`, `className`) VALUES (1, NULL, 'task 1', 'class name 1');
  </code></pre>
  
  <p>So finally questions:  </p>
  
  <ol>
  <li>Is there a way to change owning side (of course I cannot revert relation)?  </li>
  <li>How mark <code>taskset_id</code> as NOT NULL? </li>
  </ol>