title: Doctrine 2: weird behavior while batch processing inserts of entities that reference other entities
body: <p>I am trying out the batch processing method described here:
  <a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/batch-processing.html" rel="noreferrer">http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/batch-processing.html</a></p>
  
  <p>my code looks like this</p>
  
  <pre><code>    $limit = 10000;
      $batchSize = 20;
      $role = $this-&gt;em-&gt;getRepository('userRole')-&gt;find(1);
      for($i = 0; $i &lt;= $limit; $i++)
      {
          $user = new \Entity\User;
          $user-&gt;setName('name'.$i);
          $user-&gt;setEmail('email'.$i.'@email.blah');
          $user-&gt;setPassword('pwd'.$i);
          $user-&gt;setRole($role);
          $this-&gt;em-&gt;persist($user);
           if (($i % $batchSize) == 0) {
               $this-&gt;em-&gt;flush();
               $this-&gt;em-&gt;clear();
          }
      }
  </code></pre>
  
  <p>the problem is, that after the first call to em->flush() also the
  $role gets detached and for every 20 users a new role with a new id is
  created, which is not what i want</p>
  
  <p>is there any workaround available for this situation? only one i could make work is to fetch the user role entity every time in the loop </p>
  
  <p>thanks </p>