title: Hibernate complains for null @Id in @OneToOne even if it is not null
body: <p>I am trying to achieve <code>@OneToOne</code> association, using the same <code>@Id</code> between a <code>Car</code> and a <code>Person</code>. A <code>Person</code> is has an optional <code>Car</code>, but a <code>Car</code> has a required <code>Person</code> (hence I need a foreign key inside "cars" table pointing an existing <code>Person</code>):</p>
  
  <pre><code>@Entity
  @Table(name = "persons")
  public class Person {
  
      @Id
      @Column(name = "name")
      private String name;
  
      @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "person")
      private Car car;
  
      /**
       * For hibernate
       */
      @SuppressWarnings("unused")
      private Person() {
      }
  
      public Person(String name) {
          super();
          this.name = name;
      }
  
      public Car getCar() {
          return car;
      }
  
      public void setCar(Car car) {
          this.car = car;
      }
  
      public String getName() {
          return name;
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Entity
  @Table(name = "cars")
  public class Car {
      @Id
      @Column(name = "name")
      private String name;
  
      @MapsId
      @OneToOne
      @JoinColumn(name = "name")
      @OnDelete(action = OnDeleteAction.CASCADE)
      private Person person;
  
      public Car() {
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Person getPerson() {
          return person;
      }
  
      public void setPerson(Person person) {
          this.person = person;
      }
  
  }
  </code></pre>
  
  <pre><code>public interface PersonRepository extends CrudRepository&lt;Person, String&gt; {
  
  }
  </code></pre>
  
  <p>My test case:</p>
  
  <pre><code>@Autowired
  private PersonRepository personRepository;
  
  @Test
  public void test() {
      Person mike = new Person("Mike");
      personRepository.save(mike); //Saved sucessfully
  
      Person alice = new Person("Alice");
      Car car = new Car();
      car.setPerson(alice);
      car.setName(alice.getName());
      alice.setCar(car);
      personRepository.save(alice); //error
  }
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <blockquote>
    <p>org.springframework.orm.jpa.JpaSystemException: attempted to assign id
    from null one-to-one property [com.project.entity.Car.person];
    nested exception is org.hibernate.id.IdentifierGenerationException:
    attempted to assign id from null one-to-one property
    [com.project.entity.Car.person]</p>
  </blockquote>
  
  <p>I followed <a href="https://stackoverflow.com/a/36528193/6579265">this solution</a> on how to have a shared Id. The schema in database looks to be fine, and works exactly as I want. If i delete a Car, nothing happens. If I delete a person who owns a <code>Car</code>, his <code>Car</code> will be deleted from database as well. </p>
  
  <p>The question is simple. <strong>What do I do wrong?</strong></p>
  
  <p>In case it plays any role (I doubt it), this is my datasource properties:</p>
  
  <pre><code>spring.jpa.hibernate.dll-auto = create-drop
  spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&amp;characterEncoding=utf-8
  spring.datasource.username=root
  spring.datasource.password=root
  spring.datasource.platform=org.hibernate.dialect.MySQL5InnoDBDialect
  spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
  </code></pre>