title: JPA persist object in a ManyToOne relationship
body: <p>I have a company/employee <code>@OneToMany</code> relation in my database defined as:</p>
  
  <pre><code>@Entity
  public class Employee {
     @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
     private long id;
     @ManyToOne @JoinColumn(name="companyid")
     Company company;
     ....
  }
  
  @Entity
  public class Company {
  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;
  
  ...
  }
  </code></pre>
  
  <p>Now I am adding a newly created employee to a detached company. The code I use is something like:   </p>
  
  <pre><code>Company company = em1.find(Company.class, 555L);
  em1.close();
  
  EntityTransaction et = em2.getTransaction();
  et.begin();
  Employee employee = new Employee();
  employee.company = company;
  em2.persist(employee);
  et.close();
  </code></pre>
  
  <p>Will this work ok?<br>
  Is hibernate going to merge the company into the 2nd EntityManager or just use its id and persist the employee object?<br>
  Might hibernate somehow duplicate my company object or throw an exception saying that a company with the same id already exists in the DB?</p>