title: Hibernate how to add a new row to a table with a foreign key
body: <p>I am using Java8 with Hibernate and MySQL.</p>
  
  <p>I have the following tables with a join table:</p>
  
  <pre><code>+-------+     +----------------+     +------------+
  | job   |     |   person_job   |     |   person   | 
  +-------+     +----------------+     +------------+
  |  ID   |     |     PER_ID     |     |     ID     | 
  |       |     |   JOB_ID (PK)  |     +------------+
  +-------+     +----------------+   
  </code></pre>
  
  <p>(A <code>PERSON</code> can have many <code>JOBs</code>)</p>
  
  <p>When I try save a new <code>JOB</code>, it has a foreign key join to and existing <code>PERSON</code>. It looks like Hibernate wants to also save a new <code>PERSON</code>, resulting in a duplicate entry.  I thought Hibernate would be smart enough, that if there is already a matching <code>PERSON</code>, it won't try save it again.</p>
  
  <p>Resulting in the following error when trying to save a row in the <code>JOB</code> table:</p>
  
  <blockquote>
  <pre><code>MySQLIntegrityConstraintViolationException: Duplicate entry '338-1688' for key 'PRIMARY'
  </code></pre>
  </blockquote>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>SELECT * FROM ebdb.person_job;
  
  PER_ID    JOB_ID
   338        16
  </code></pre>
  
  <p>and</p>
  
  <pre><code>SELECT * FROM ebdb.person
  
   ID
   338
  </code></pre>
  
  <p>and </p>
  
  <pre><code>SELECT * FROM ebdb.job;
  
   ID
   16
  </code></pre>
  
  <p><strong>Job.java</strong></p>
  
  <pre><code>@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinTable(name = "person_job", joinColumns = {
          @JoinColumn(name = "JOB_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
                  @JoinColumn(name = "PER_ID", referencedColumnName = "ID") })
  private Person person;
  </code></pre>
  
  <p>When I run in debug mode, I see the new <code>Job</code> row it is trying to <code>merge</code> has an <code>ID</code> of <code>26</code> and a <code>PERSON</code> with an <code>ID</code> of <code>338</code> as expected.</p>
  
  <pre><code>protected T merge(T entity) throws InstantiationException, IllegalAccessException {
      T attached = null;
      if (entity.getId() != null) {
          attached = entityManager.find(entityClass, entity.getId());
      }
      if (attached == null) {
          attached = entityClass.newInstance();
      }
  
      BeanUtils.copyProperties(entity, attached);
      entityManager.setFlushMode(FlushModeType.COMMIT);
       attached = entityManager.merge(attached);
  
      return attached;
  }
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>How do you create a new entry on one table (<code>JOB</code>), that has a foreign key join to an existing entry (<code>PERSON</code>)?</p>
  
  <p>i.e. I want to just maintain a <code>@ManyToOne</code> relationship.</p>
  
  <p>Please can anyone assist.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If I try <code>persist</code> instead of <code>merge</code>, I get:</p>
  
  <blockquote>
    <p>detached entity passed to persist: com.jobs.spring.domain.Person</p>
  </blockquote>