title: Spring Data JPA - Deleting a child in one-to-many relationship
body: <p>I'm implementing a Spring boot application and using Spring Data JPA in it. As you know you don't have to implement the repository interface for just CRUD methods, because Spring Data JPA creates an implementation on the fly. So I have just this:</p>
  
  <pre><code>public interface PersonRepository extends JpaRepository&lt;Person, Long&gt; {}
  </code></pre>
  
  <p>I'm working with one-to-many relationship, this is in my Person domain:</p>
  
  <pre><code>@OneToMany(cascade = CascadeType.ALL,
          orphanRemoval = true,
          fetch = FetchType.LAZY,
          mappedBy = "person")
  private Set&lt;Contact&gt; contacts = new HashSet&lt;&gt;();
  </code></pre>
  
  <p>I decided to write an integration test for child removal from the parent: </p>
  
  <pre><code>    @Test
      public void removeFromContacts() {
      // given
      Person person = new Person ("test person");       
      Contact contact = new Contact("test@gmail.com", "+123456789");
      contact.setPerson(person);
      person.getContacts().add(contact);
  
      personRepository.save(person);
  
      Person savedPerson = personRepository.findOne(person.getId());
      Contact persistedContact = savedPerson.getContacts().stream().findFirst().orElse(null);
  
      // when
      savedPerson.getContacts().remove(persistedContact);
      persistedContact.setPerson(null);
      Person edited = personRepository.save(savedPerson);
      // then
  
      Assert.assertTrue(edited.getContacts().isEmpty());
  }
  </code></pre>
  
  <p>This test fails. The reason is <code>savedPerson.getContacts().remove(persistedContact)</code> line doesn't change anything, <code>remove</code> method returns false. It's pretty strange, because I'm trying to remove an object from a hash set which has only one object with exact same hash code (<code>equals()</code> method returns true as well). According to <a href="https://stackoverflow.com/a/43554123/5718409">this</a> answer the contact object could've been altered somehow after adding it to the hash set. The only thing I can think of is it happened after this line: <code>personRepository.save(person)</code>.</p>
  
  <p>If I'm right then I'm really confused: how should I remove the contact from a person, and even if I find a way, is it okay for <code>personRepository.save</code> method to cause a set to malfunction? And if I'm wrong I would love to know the right answer.</p>
  
  <p>Thanks in advance.</p>