title: How to insert multiple records with JPA repositories in a table with a composed PK and one of it is auto incremented?
body: <p>I'm building a controller with spring boot to store a list of records, but the table where the records will be stored has a composed PK (two fields) and one of the fields of the composition is auto_incrementable, my entities are defined as follow:</p>
  
  <p><strong>TaskPK.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Embeddable
  public class TaskPK implements Serializable {
      //default serial version id, required for serializable classes.
      private static final long serialVersionUID = 1L;
  
      @Column(name="taskflow_template_task_id", unique=true, nullable=false)
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer taskflowTemplateTaskId;
  
      @Column(name="taskflow_template_id", insertable=false, updatable=false, unique=true, nullable=false)
      private Integer taskflowTemplateId;
  
  // Getters, setters and constructors
  }
  </code></pre>
  
  <p>In this PK the taskflowTemplateTaskId is an AI field</p>
  
  <p><strong>Task.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class Task extends BaseEntity implements Serializable, Persistable&lt;TaskPK&gt; {
      private static final long serialVersionUID = 1L;
  
      @EmbeddedId
      private TaskPK id = new TaskPK();
  
      @Column(name="task_id")
      private Integer taskId;
  
      @Column(name="display_order", nullable=false)
      private int displayOrder;
  
      //bi-directional many-to-one association to TaskflowTemplate
      @ManyToOne
      @JoinColumn(name="taskflow_template_id", nullable=false, insertable=false, updatable=false)
      private TaskflowTemplate taskflowTemplate;
  
      @Override
      public boolean isNew() {
          return (id.getTaskflowTemplateTaskId() == null || id.getTaskflowTemplateId() == null);
      }
  </code></pre>
  
  <p>The task entity has a spring data JPA repositories associated without any custom queries</p>
  
  <p>Finally, this is the code i'm using to store the data, is a simple function called from the controller</p>
  
  <pre class="lang-java prettyprint-override"><code>@Transactional
  public void createTask(List&lt;Task&gt; tasks){
      taskRepository.saveAll(tasks)
  }
  
  </code></pre>
  
  <p>And the data I'm receiving is represented in the following JSON:</p>
  
  <pre><code>[
    {
      "displayOrder": 1,
      "taskId": 1,
      "id": {
          "taskflowTemplateId" : "1"
       }
    },
    {
      "displayOrder": 2,
      "taskId": 2,
      "id": {
          "taskflowTemplateId" : "1"
       }
    },
    {
      "displayOrder": 3,
      "taskId": 3,
      "id": {
          "taskflowTemplateId" : "1"
       }
    }
  ]
  </code></pre>
  
  <p>When i run this code i get this exception</p>
  
  <pre><code>javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.test.taskmaster.entity.Task#TaskPK(taskflowTemplateTaskId=null, taskflowTemplateId=1)]
  </code></pre>
  
  <p>So, I'm guessing the field taskflowTemplateTaskId is not being generated by hibernate, instead it is sending as null. When only one record is created this works well because the null is replaced on database by the AI field, but when i try to insert a list of records all with null taskflowTemplateTaskId because i want it to be generated, that error happens.</p>
  
  <p>I've tried using save inside a loop, and saveAndFlush without success, also i've tried different type of GenerationType and even using @Generated(GenerationTime).</p>