title: Doctrine 2 insists on persisting already managed entity in ManyToOne relationship
body: <p>I have a database table containing departments. I have another table containing people. As you'd expect a department contains many people, and a person is in a single department.</p>
  
  <p>When I want to persist a new person to the database, I create a Person object and try to set it's Department property to an <strong>existing</strong> Department object which is managed by the Entity Manager. However, when I try to persist my new Person, I get an exception:</p>
  
  <blockquote>
    <p>A new entity was found through the relationship
    'Entities\Person#department' that was not configured to cascade
    persist operations for entity:
    Entities\Department@0000000016abe202000000000d29dd37. To solve this
    issue: Either explicitly call EntityManager#persist() on this unknown
    entity or configure cascade persist  this association in the mapping
    for example @ManyToOne(..,cascade={"persist"}).</p>
  </blockquote>
  
  <p>I don't fully understand the part of the exception which says the department is an "unknown entity", since I extracted it via the Entity manager.</p>
  
  <p>As the exception suggests, I inserted a cascade into the yml metadata (<code>cascade: ["persist"]</code>). My person then gets saved, but I end up with a <strong>duplicated Department</strong> in the departments table, with a new id.</p>
  
  <p>This must be a very common use case. I've included my code and metadata below. What changes should I make?</p>
  
  <p>Metadata:</p>
  
  <pre><code>Entities\Person
    type: entity
    table: people
    fields:
      ...
      departmentId:
        type: integer
        unsigned: false
        nullable: false
        column: department_id
      ...
    manyToOne:
      department:
        targetEntity: Entities\Department
        joinColumn: department_id
        referenceColumnName: id
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>$department = $em-&gt;getRepository('Department')-&gt;findOneBy(array('name' =&gt; $departmentName);
  
  $person = new Person();
  $person-&gt;setName('Joe Bloggs');
  $person-&gt;setDepartment($department);
  
  $em-&gt;persist($person);
  $em-&gt;flush();
  </code></pre>