Hibernate attempted to assign id from null one-to-one property
<p>I have two entities:</p>

<pre><code>@Entity
@Table
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
    private UserDetails userDetails;

}

@Entity
@Table(name="user_details")
public class UserDetails {

    @GenericGenerator(name = "generator", strategy = "foreign",
        parameters = @Parameter(name = "property", value = "user"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(unique = true, nullable = false)
    private Integer id;

    @OneToOne
    @PrimaryKeyJoinColumn
    private User user;

    public UserDetails(User user) {
        this.user = user;
        user.setUserDetails(this);
    }

}
</code></pre>

<p>It works if I create a user with a userDetails. But then it creates a UserDetails row and I don't want it. I have to fetch a User from database and add a UserDetails later:</p>

<pre><code>userRepository.findOneById(id).map(user -&gt; {
    UserDetails userDetails = user.getUserDetails();
    if (userDetails == null)
        userDetails = new UserDetails(user); 
     userDetails.setEmail(email);
     userRepository.save(user); //error here
});
</code></pre>

<p>Error</p>

<blockquote>
  <p>org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [com.app.domain.UserDetails.user]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.app.domain.UserDetails.user]</p>
</blockquote>