title: How to remove usage of cascade={"remove","persist"} from entities in Symfony2?
body: <p>I am working on a project where there I am forced to use cascade={"remove","persist"} because of the <a href="http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/faq.html#what-is-wrong-when-i-get-an-invalidargumentexception-a-new-entity-was-found-through-the-relationship" rel="nofollow">problem described here.</a></p>
  
  <p>Reading through the <a href="http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations" rel="nofollow">documentation</a>, to quote:</p>
  
  <blockquote>
    <p>Even though automatic cascading is convenient it should be used with care. Do not blindly apply cascade=all to all associations as it will unnecessarily degrade the performance of your application. For each cascade operation that gets activated Doctrine also applies that operation to the association, be it single or collection valued.</p>
  </blockquote>
  
  <p>And I see that the same can be fixed if I use </p>
  
  <pre><code>$em-&gt;persist($entity);
  </code></pre>
  
  <p>In my persistence services, which I am already calling. However, doctrine doesn't work as expected. Here are my entities.</p>
  
  <p><strong>Entity/Employee.php</strong></p>
  
  <pre><code>&lt;?php
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping AS ORM;
  
  /**
   * @ORM\Entity
   * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(
   *             name="UNIQ_EMPLOYEE_ID_NAME_ADDRESS_STATE_CITY_COUNTRY",
   *             columns={"id","name","city","state","country"}
   *         )})
   */
  class Employee
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer", options={"unsigned":true})
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=false)
       */
      private $name;
  
      /**
       * @ORM\Column(type="text", nullable=false)
       */
      private $address;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=false)
       */
      private $city;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=false)
       */
      private $state;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=false)
       */
      private $country;
  
  
      /**
       * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Department", inversedBy="employee", cascade={"remove","persist"})
       * @ORM\JoinColumn(name="department_id", referencedColumnName="id", nullable=false)
       */
      private $department;
  
      /**
       * @ORM\OneToMany(targetEntity="AppBundle\Entity\Transfer", mappedBy="employee", cascade={"remove","persist"})
       */
      private $transfer;
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>Entity/Department.php</strong></p>
  
  <pre><code>&lt;?php
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping AS ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use Doctrine\Common\Collections\Selectable;
  
  /**
   * @ORM\Entity
   * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(
   *             name="UNIQ_DEPARTMENT_ID_NAME",
   *             columns={"id","name"}
   *         )})
   */
  class Department
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer", options={"unsigned":true})
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=false)
       */
      private $name;
  
      /**
       * @ORM\OneToMany(targetEntity="AppBundle\Entity\Employee", mappedBy="department", cascade={"remove","persist"})
       */
      private $employee;
  
      /**
       * @ORM\OneToMany(targetEntity="AppBundle\Entity\Transfer", mappedBy="department", cascade={"remove","persist"})
       */
      private $transfer;
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>Entity/Transfer.php</strong></p>
  
  <pre><code>&lt;?php
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping AS ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use Doctrine\Common\Collections\Selectable;
  
  /**
   * @ORM\Entity
   * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(
   *             name="UNIQ_TRANSFER_ID_DEPARTMENT_EMPLOYEE_START_END",
   *             columns={"id","name"}
   *         )})
   */
  class Transfer
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer", options={"unsigned":true})
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(type="date", length=255, nullable=false)
       */
      private $start;
  
      /**
       * @ORM\Column(type="date", length=255, nullable=false)
       */
      private $end;
  
      /**
       * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee", inversedBy="attendance", cascade={"persist","remove"})
       * @ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
       */
      private $employee;
  
      /**
       * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Department", inversedBy="attendance", cascade={"persist","remove"})
       * @ORM\JoinColumn(name="department_id", referencedColumnName="id", nullable=false)
       */
      private $department;
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>Now, I have another problem. Since my GeneratorValue strategy for Employee and Department are NONE, I have problem with duplicate record error. I am trying to use PreFlushEventArgs to remove the entity before persisting if the record exists in database. But I wonder if it should be that complex?</p>
  
  <p>Thanks for your help in advance.</p>