title: Spring JPA get error detached entity passed to persist on unidirectional OneToOne relationship when save to repository without cascade
body: <p>I have two entity: Person and Patient. The Patient is a child of Person and it maps its primary key to Person's primary key. This was meant to be a unidirectional OneToOne relation so Person doesn't know about Patient.</p>
  <p>The Person is already persisted in the PostgreSQL, but when I tried to persist the Patient, I got this error:</p>
  <pre><code>Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.example.clinic.entities.Person; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.clinic.entities.Person
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:297) ~[spring-orm-5.3.12.jar:5.3.12]
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233) ~[spring-orm-5.3.12.jar:5.3.12]
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551) ~[spring-orm-5.3.12.jar:5.3.12]
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.3.12.jar:5.3.12]
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.3.12.jar:5.3.12]
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152) ~[spring-tx-5.3.12.jar:5.3.12]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.12.jar:5.3.12]
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) ~[spring-data-jpa-2.5.6.jar:2.5.6]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.12.jar:5.3.12]
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.12.jar:5.3.12]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.12.jar:5.3.12]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.12.jar:5.3.12]
      at jdk.proxy2/jdk.proxy2.$Proxy89.save(Unknown Source) ~[na:na]
      at com.example.clinic.ClinicApplication.lambda$demo$0(ClinicApplication.java:43) ~[classes/:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791) ~[spring-boot-2.5.6.jar:2.5.6]
      ... 5 common frames omitted
  </code></pre>
  <p>This is a snippet of my Person Class</p>
  <pre><code>@Entity
  @Table(name = &quot;person&quot;)
  public class Person {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = &quot;user_id&quot;)
      private Long userId;
  
      @Column(name = &quot;first_name&quot;)
      private String firstName;
      @Column(name = &quot;last_name&quot;)
      private String lastName;
  
      private String email;
  
      @Column(name = &quot;created_on&quot;)
      private Date createdOn;
  }
  </code></pre>
  <p>The Patient class:</p>
  <pre><code>@Entity
  @Table(name = &quot;patient&quot;)
  public class Patient {
      @Id
      private Long id;
  
      @OneToOne
      @MapsId
      @JoinColumn(name = &quot;user_id&quot;)
      private Person person;
  
      @Column(name = &quot;last_visit&quot;)
      private Date lastVisit;
  }
  </code></pre>
  <p>the Patient repo:</p>
  <pre><code>@Repository
  public interface PatientRepository extends JpaRepository&lt;Patient, Long&gt; {
      Patient findById(long id);
  
      @Query(&quot;SELECT p FROM Patient p WHERE p.person.email = ?1&quot;)
      Patient findByEmail(String email);
  }
  </code></pre>
  <p>EDIT:</p>
  <p>Here is where I tried to save the Patient</p>
  <pre><code>Person person = new Person(&quot;patient&quot;, &quot;pat&quot;, &quot;patient@email.com&quot;);
  personRepository.save(person);
  Patient patient1 = new Patient(person);
  patientRepository.save(patient1); // &lt;---- here is the exception happen
  </code></pre>
  <p>Any idea?</p>