title: Removing child Entity from Table also removes parent in it's table
body: <p>I have 2 entities one of which has a foreign primary key from the other, I've got it working such that I can add entities to my database and connect them to the child. However, when I try to remove an entry from the child table it also removes the associated entry in the parent. I'm currently using a sample program to make sure all my relationships are working as expected, the bean for which is:</p>
  <pre><code>@Bean
  public CommandLineRunner test(ContactRepository contactRepository, EstateAgentRepository estateAgentRepository) {
      return args -&gt; {
          contactRepository.deleteAll();
          estateAgentRepository.deleteAll();
  
          var contact1 = new Contact(&quot;Johndoe@gmail.com&quot;, &quot;John&quot;, &quot;Doe&quot;, &quot;07587102936&quot;);
          var contact2 = new Contact(&quot;Joeblogs@yahoo.co.uk&quot;, &quot;Joe&quot;, &quot;Blogs&quot;, &quot;01635435679&quot;);
          var contact3 = new Contact(&quot;Janeblogs@yahoo.co.uk&quot;, &quot;Jane&quot;, &quot;Blogs&quot;, &quot;01635435679&quot;);
          contactRepository.save(contact1);
          contactRepository.save(contact2);
          contactRepository.save(contact3);
  
          System.out.println(&quot;Contacts found with findAll():&quot;);
          contactRepository.findAll().forEach(contact -&gt; System.out.println(contact.toString()));
          System.out.println();
  
          // Makes Blogs Family Estate Agents
          contactRepository.findContactsByLastName(&quot;Blogs&quot;).forEach(blogs -&gt; {
              var estateAgent = new EstateAgent(blogs, &quot;Blogs Family Homes&quot;);
              blogs.setEstateAgent(estateAgent);
              estateAgentRepository.save(estateAgent);
          });
          System.out.println();
  
          System.out.println(&quot;Estate Agents&quot;);
          estateAgentRepository.findAll().forEach(estateAgent -&gt; System.out.println(estateAgent.toString()));
          System.out.println();
  
          // Removes Joe Blogs as an estate agent
          var removeAgent = estateAgentRepository.findEstateAgentByEmail(&quot;Joeblogs@yahoo.co.uk&quot;);
          estateAgentRepository.delete(removeAgent);
  
          System.out.println(&quot;Contacts found with findAll():&quot;);
          contactRepository.findAll().forEach(contact -&gt; System.out.println(contact.toString()));
          System.out.println();
  
          System.out.println(&quot;Estate Agents&quot;);
          estateAgentRepository.findAll().forEach(estateAgent -&gt; System.out.println(estateAgent.toString()));
          System.out.println();
      };
  }
  </code></pre>
  <p>The Contact entity is as follows:</p>
  <pre><code>@Entity
  @Table(name = &quot;contact&quot;)
  public class Contact {
  
      @Id
      private String email;
      @Column(name = &quot;first_name&quot;)
      private String firstName;
      @Column(name = &quot;last_name&quot;)
      private String lastName;
      @Column(name = &quot;phone&quot;)
      private String phoneNumber;
      @OneToOne(mappedBy = &quot;contact&quot;, cascade = CascadeType.ALL, orphanRemoval = true)
      private EstateAgent estateAgent;
      
      // other fields
  
      protected Contact() {}
  
      public Contact(String email, String firstName, String lastName, String phoneNumber) {
          this.email = email;
          this.firstName = firstName;
          this.lastName = lastName;
          this.phoneNumber = phoneNumber;
      }
      
      // Getters and Setters
  
      public void setEstateAgent(EstateAgent estateAgent) {
          if (estateAgent == null) {
              if (this.estateAgent != null) {
                  this.estateAgent.setContact(null);
              }
          } else {
              estateAgent.setContact(this);
          }
          this.estateAgent = estateAgent;
      }
  }
  </code></pre>
  <p>My Estate Agent Entity is:</p>
  <pre><code>@Entity
  @Table(name = &quot;estate_agent&quot;)
  public class EstateAgent {
  
      @Id
      @Column(name = &quot;estate_agent_email&quot;)
      private String email;
      @OneToOne(cascade = CascadeType.ALL)
      @MapsId
      @JoinColumn(name = &quot;estate_agent_email&quot;, referencedColumnName = &quot;email&quot;)
      private Contact contact;
      @Column(name = &quot;company_name&quot;)
      private String companyName;
      
      protected EstateAgent() {}
  
      public EstateAgent(Contact contact, String companyName) {
          this.contact = contact;
          email = contact.getEmail();
          this.companyName = companyName;
      }
      
      // Getters and Setters
  }
  </code></pre>
  <p>My understanding of cascade ALL is that it is likely what's causing the entry to be deleted in the Contact Table due to being on the @OneToOne Annotation in Estate Agent and when I run it, it prints out the following:</p>
  <blockquote>
  <p>Contacts found with findAll():
  Contact{email='Janeblogs@yahoo.co.uk', firstName='Jane', lastName='Blogs', phoneNumber='01635435679'}
  Contact{email='Joeblogs@yahoo.co.uk', firstName='Joe', lastName='Blogs', phoneNumber='01635435679'}
  Contact{email='Johndoe@gmail.com', firstName='John', lastName='Doe', phoneNumber='07587102936'}</p>
  <p>Estate Agents
  EstateAgent{email='Janeblogs@yahoo.co.uk', companyName='Blogs Family Homes'}
  EstateAgent{email='Joeblogs@yahoo.co.uk', companyName='Blogs Family Homes'}</p>
  <p>Contacts found with findAll():
  Contact{email='Janeblogs@yahoo.co.uk', firstName='Jane', lastName='Blogs', phoneNumber='01635435679'}
  Contact{email='Johndoe@gmail.com', firstName='John', lastName='Doe', phoneNumber='07587102936'}</p>
  <p>Estate Agents
  EstateAgent{email='Janeblogs@yahoo.co.uk', companyName='Blogs Family Homes'}</p>
  </blockquote>
  <p>As you can see this is adding the people to the Contacts and EstateAgents correctly, but when it tries to remove joe blogs from Estate Agents it also removes him from Contacts.</p>
  <p>However, if I remove the cascade annotation I'm met with the following when I attempt to run the Application:</p>
  <blockquote>
  <p>Contacts found with findAll():
  Contact{email='Janeblogs@yahoo.co.uk', firstName='Jane', lastName='Blogs', phoneNumber='01635435679'}
  Contact{email='Joeblogs@yahoo.co.uk', firstName='Joe', lastName='Blogs', phoneNumber='01635435679'}
  Contact{email='Johndoe@gmail.com', firstName='John', lastName='Doe', phoneNumber='07587102936'}</p>
  <p>2021-04-27 19:47:44.480 ERROR 2800 --- [           main] org.hibernate.AssertionFailure           : HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null identifier</p>
  </blockquote>
  <p>With the following StackTrace:</p>
  <pre><code>org.hibernate.AssertionFailure: null identifier
      at org.hibernate.engine.spi.EntityKey.&lt;init&gt;(EntityKey.java:51) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.internal.AbstractSharedSessionContract.generateEntityKey(AbstractSharedSessionContract.java:536) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.type.OneToOneType.isNull(OneToOneType.java:105) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.type.EntityType.resolve(EntityType.java:462) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.type.EntityType.resolve(EntityType.java:457) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.type.EntityType.replace(EntityType.java:358) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.type.AbstractType.replace(AbstractType.java:164) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.type.TypeHelper.replace(TypeHelper.java:204) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:488) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:241) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:318) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:172) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:70) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:93) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:793) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:780) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311) ~[spring-orm-5.3.4.jar:5.3.4]
      at com.sun.proxy.$Proxy84.merge(Unknown Source) ~[na:na]
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:560) ~[spring-data-jpa-2.4.5.jar:2.4.5]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) ~[spring-data-commons-2.4.5.jar:2.4.5]
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.4.5.jar:2.4.5]
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.4.5.jar:2.4.5]
      at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:524) ~[spring-data-commons-2.4.5.jar:2.4.5]
      at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285) ~[spring-data-commons-2.4.5.jar:2.4.5]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:531) ~[spring-data-commons-2.4.5.jar:2.4.5]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.4.jar:5.3.4]
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:156) ~[spring-data-commons-2.4.5.jar:2.4.5]
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131) ~[spring-data-commons-2.4.5.jar:2.4.5]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.4.jar:5.3.4]
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.4.jar:5.3.4]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.4.jar:5.3.4]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.4.jar:5.3.4]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.4.jar:5.3.4]
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.4.jar:5.3.4]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.4.jar:5.3.4]
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) ~[spring-data-jpa-2.4.5.jar:2.4.5]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.4.jar:5.3.4]
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.4.jar:5.3.4]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.4.jar:5.3.4]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.4.jar:5.3.4]
      at com.sun.proxy.$Proxy90.save(Unknown Source) ~[na:na]
      at nextsteps.dev.web.NextStepsPropertyPurchaseSystemApplication.lambda$test$1(NextStepsPropertyPurchaseSystemApplication.java:37) ~[main/:na]
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) ~[na:na]
      at nextsteps.dev.web.NextStepsPropertyPurchaseSystemApplication.lambda$test$5(NextStepsPropertyPurchaseSystemApplication.java:34) ~[main/:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) ~[spring-boot-2.4.3.jar:2.4.3]
      at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) ~[spring-boot-2.4.3.jar:2.4.3]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.3.jar:2.4.3]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.3.jar:2.4.3]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.3.jar:2.4.3]
      at nextsteps.dev.web.NextStepsPropertyPurchaseSystemApplication.main(NextStepsPropertyPurchaseSystemApplication.java:13) ~[main/:na]
  </code></pre>
  <p>I'm sure I'm making an obvious error, but for the life of me I can't see it, any help is greatly appreciated :)</p>