title: Spring CrudRepository cannot share generated id in composite primary key
body: <p>I'm trying use <em>spring CRUDRepository</em> for inserting new object into database using JPA database model (<strong>User.java</strong>, <strong>UserInfo.java</strong>). Database model are related with composite primary key (<strong>UserPK.java</strong>), where one of them is automatic generated (field named <strong>id</strong>) and second (field named <strong>type</strong>) is set in beginning.</p>
  
  <p>And I get errors when I create new object with using <em>CRUDRepository</em> (<strong>UserRepository.java</strong>) - cannot insert new object into second model (<strong>UserInfo.java</strong>), because <strong>id</strong> is null (first model was added correctly). I think problem is in sharing/mapping composite primary key in database model. 
  I tried the same model with <em>EntityManager</em> and it wasn't error - all was added. Next I used <em>@PrimaryKeyJoinColumns</em> annotation, but with that same result as above (but there I'm not sure that I used it correctly) - <em>CRUDRepository</em> failed and <em>EntityManager</em> success.</p>
  
  <p>Can anyone help me find solution? I add also source code on <a href="https://github.com/kaczla/SpringBootCompositePrimaryKey" rel="nofollow noreferrer">GitHub</a> if someone wants to run code.</p>
  
  <p><strong>Logs below:</strong></p>
  
  <p>Log <em>CRUDRepository</em></p>
  
  <pre><code>Hibernate: select user0_.id as id1_0_1_, user0_.type as type2_0_1_, user0_.email as email3_0_1_, user0_.login as login4_0_1_, userinfo1_.id as id3_1_0_, userinfo1_.type as type4_1_0_, userinfo1_.name as name1_1_0_, userinfo1_.surname as surname2_1_0_ from user user0_ left outer join user_info userinfo1_ on user0_.id=userinfo1_.id and user0_.type=userinfo1_.type where user0_.id=? and user0_.type=?
  Hibernate: call next value for seq_id
  Hibernate: select userinfo0_.id as id3_1_0_, userinfo0_.type as type4_1_0_, userinfo0_.name as name1_1_0_, userinfo0_.surname as surname2_1_0_ from user_info userinfo0_ where userinfo0_.id=? and userinfo0_.type=?
  Hibernate: insert into user (email, login, id, type) values (?, ?, ?, ?)
  Hibernate: insert into user_info (name, surname, id, type) values (?, ?, ?, ?)
   WARN 17653 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
  ERROR 17653 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL not allowed for column "ID"; SQL statement:
  insert into user_info (name, surname, id, type) values (?, ?, ?, ?) [23502-196]
  ...
  </code></pre>
  
  <p>Log <em>EntityManager</em></p>
  
  <pre><code>Hibernate: call next value for seq_id
  Hibernate: insert into user (email, login, id, type) values (?, ?, ?, ?)
  Hibernate: insert into user_info (name, surname, id, type) values (?, ?, ?, ?)
  </code></pre>
  
  <p><strong>Code below:</strong></p>
  
  <p>Main model: <strong>User.java</strong>
  </p>
  
  <pre><code>import com.fasterxml.jackson.annotation.JsonManagedReference;
  import javax.persistence.*;
  import javax.validation.constraints.NotNull;
  import java.io.Serializable;
  
  @Entity
  @Table(name = "USER")
  @IdClass(UserPK.class)
  public class User implements Serializable {
  
      @OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
      @JsonManagedReference
      private UserInfo info;
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ID")
      @SequenceGenerator(name = "SEQ_ID", sequenceName = "SEQ_ID", allocationSize = 1)
      @NotNull
      @Column(name = "ID")
      private Long id;
      @Id
      @NotNull
      @Column(name = "TYPE")
      private String type;
      @NotNull
      @Column(name = "LOGIN")
      private String login;
      @NotNull
      @Column(name = "EMAIL")
      private String email;
      /* ... */
  }
  </code></pre>
  
  <p>Second model: <strong>UserInfo.java</strong>
  </p>
  
  <pre><code>import com.fasterxml.jackson.annotation.JsonBackReference;
  import javax.persistence.*;
  import java.io.Serializable;
  
  @Entity
  @Table(name = "USER_INFO")
  public class UserInfo implements Serializable {
  
      @Id
      @OneToOne(cascade = CascadeType.ALL)
      @JoinColumns({
       @JoinColumn(name = "id", referencedColumnName = "id"),
       @JoinColumn(name = "type", referencedColumnName = "type")
      })
      @JsonBackReference
      @MapsId
      private User user;
  
      @Column(name = "NAME")
      private String name;
      @Column(name = "SURNAME")
      private String surname;
      /* ... */
  }
  </code></pre>
  
  <p>Composed primary key: <strong>UserPK.java</strong>
  </p>
  
  <pre><code>import java.io.Serializable;
  
  public class UserPK implements Serializable {
      private Long id;
      private String type;
      /* ... */
  }
  </code></pre>
  
  <p><em>spring CRUDRepository</em>: <strong>UserRepository.java</strong>
  </p>
  
  <pre><code>import org.springframework.data.repository.CrudRepository;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface UserRepository extends CrudRepository&lt;User, UserPK&gt; {
      User findByIdAndAndType(Long id, String type);
  }
  </code></pre>
  
  <p>Repository using <em>EntityManager</em>: <strong>UserRepositoryEM.java</strong>
  </p>
  
  <pre><code>import org.springframework.stereotype.Repository;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.transaction.Transactional;
  
  @Repository
  @Transactional
  public class UserRepositoryEM {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      public User findByKey(UserPK key) {
          return entityManager.find(User.class, key);
      }
  
      public User save(User user) {
          entityManager.persist(user);
          entityManager.flush();
          return user;
      }
  }
  </code></pre>