title: How to create composite primary key with one auto generated value?
body: <pre><code>I am trying to map entity with my existing database table which is having two primary keys.
  
  Out of two keys, one primary key is auto generated.
  
  I am using `Spring Boot`, `JPA`, `Hibernate` and `MySQL`. I have used `@IdClass` to map with the composite primary class (with public constructor, setter/getters, same property names, equals and hash code methods).
  
  `org.springframework.data.repository.CrudRepository` save method to save the entity.  
  </code></pre>
  
  <p>Code snippet below.</p>
  
  <pre><code>@Entity
  @Table(name = "data")
  @IdClass(DataKey.class)
  public class DeviceData {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="id")
      private BigInteger id;
      @Column(name="name")
      private String name;
      @Id
      @Column(name="device_id")
      private int deviceId;
      getters/setters
  }
  public class DataKey implements Serializable {
      private static final long serialVersionUID = 1L;
      private BigInteger id;
      private int deviceId;
      //setter/getters
      public DataKey() {
      }
      public DataKey(BigInteger id, int deviceId) {
          this.id = id;
          this.deviceId = deviceId;
      }
      public int hashCode() {
          return Objects.hash(id, deviceId);
      }
      public boolean equals(Object obj) {
          if (obj == this)
              return true;
          if (!(obj instanceof DataKey))
              return false;
          DataKey dk = (DataKey) obj;
          return dk.id.equals(this.id) &amp;&amp; dk.deviceId == (this.deviceId);}
  }
  I am using org.springframework.data.repository.CrudRepository save method for persisting the entity.
  </code></pre>
  
  <p>DeviceData data=new DeviceData();</p>
  
  <p>data.setName("device1");
  data.setDeviceId("1123");</p>
  
  <p>dao.save(data); //dao extending crudrepository interface.</p>
  
  <p>But I am getting below errors :</p>
  
  <pre><code>    org.springframework.orm.jpa.JpaSystemException: Could not set field 
      value [POST_INSERT_INDICATOR] value by reflection.[class DataKey.id]
      setter of DataKey.id; nested exception is
      org.hibernate.PropertyAccessException: Could not set field value
      [POST_INSERT_INDICATOR] value by reflection : [class class DataKey.id]
      setter of class DataKey.id.
  </code></pre>
  
  <p>Caused by: java.lang.IllegalArgumentException: Can not set java.math.BigInteger field DataKey.id to org.hibernate.id.IdentifierGeneratorHelper$2</p>