title: Problems with em.persist on new entity with existing but detached OneToOne entity
body: <p>I use Hibernate on JBoss AS 7.1 and have problems when i create new entities and set an existing entity to a ManyToOne relation. The exising entity (customer) is queried before using the following code:</p>
  
  <pre><code>TypedQuery&lt;Customer&gt; query = em.createNamedQuery("Customer.findByLastNameLike", Customer.class);
  query.setParameter("lastName", "%"+lastName+"%");
  return query.getResultList();
  </code></pre>
  
  <p>When i execute an <code>em.persist(order)</code> on the new entity <code>order</code> after setting <code>customer</code> to order with <code>order.setCustomer(customer)</code> the following exception is thrown:</p>
  
  <pre><code>Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: erp.model.persons.Customer
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:717)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:710)
      at org.hibernate.engine.spi.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:49)
      at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
      at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
      at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
      at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
      at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:437)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:266)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:138)
      at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
      at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:725)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:701)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:705)
      at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:853)
  </code></pre>
  
  <p>The entities are defined as follows:</p>
  
  <pre><code>@Entity
  public class Order extends AbstractEntity implements Serializable {
  
      private static final long serialVersionUID = -8061887078955032972L;
  
      @ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST}, optional = false)
      private Customer customer = null;
  
  @Entity
  @DiscriminatorValue("customer")
  public class Customer extends Person implements Serializable {
  
      private static final long serialVersionUID = 2788107135840578322L;
  
      @OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
      private List&lt;Order&gt; orders = null;
  </code></pre>
  
  <p>What is wrong in this scenario?</p>
  
  <p>Thanks for any hint...</p>