title: Duplicate entries for @OneToOne entity?
body: <p>I have a simple parent-child relation.</p>
  
  <p>Problem: in my database there are sometimes multiple child elements that map to the same parent.</p>
  
  <pre><code>@Entity
  public class PersonEntity {
      @Id
      private long id;
  
      //assume every person can only have one car
      @OneToOne(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
      private CarEntity car;
  
      private String name;
  }
  
  @Entity
  public class CarEntity {
      @Id
      private long id;
  
      @OneToOne
      @JoinColumn(name = "fk_person_id", foreignkey = @ForeignKey("name = "fk_person"))
      private PersonEntity person;
  
      private String name;
  }
  </code></pre>
  
  <p>Question: how can I happen that there are multiple <code>car</code> elements that map to the <em>same</em> person entity? How is it even possible that hibernate persists those elements? And where could they come from?</p>
  
  <p>As a result, when I fetch those person entities having multiple car mappings, I'm getting of course an exception:</p>
  
  <p><code>Caused by: org.hibernate.HibernateException: More than one row with the given identifier was found: 123, for class: CarEntity...</code></p>
  
  <p>Sidenote: I would finally want to delete the duplicate <code>CarEntity</code> and only keep the most recent one. But how can I prevent this to happen in future?</p>
  
  <p>Update:</p>
  
  <pre><code>@Transactional
  public PersonEntity createUpdatePerson(PersonDTO dto) {
      PersonEntity entity = dao.findByPersonName(dto.getName); //CrudRepository by spring
      if (entity == null) {
          entity = new Person();
      }
  
      mergeDTO(dto, entity); //copy and fill new values
      if (person.getId() == null) dao.save(entity);
      return entity;
  }
  
  private void mergeDTO(PersonDTO dto, PersonEntity entity) {
         if (dto.getCar() != null) {
               if (entity.getCar() == null) entity.setCar(new Car());
               entity.getCar().setName(dto.getCar().getName());
               entity.getCar().setPerson(entity);
         }
  }
  </code></pre>