title: Getting "Duplicate key value violates unique constraint" with Postgres
body: <p>I am working on Springboot application. <br/>
  <br/><br/>
  <strong>Problem statement:</strong> <br/>
  I am using <strong>sequence</strong> to generate id.
  When I am hitting the controller for the first time, then there is no exception, and data is successfully saved into the database. <br/>
  But when I am hitting the controller second time, then I am getting :<br/></p>
  
  <pre><code>**duplicate key violates unique constraint, Detail: 
  Key(id)=(100) already exits.** &lt;br/&gt;
  </code></pre>
  
  <p>But id = 100 was already inserted 10 records earlier.</p>
  
  <p>I have below POJO:</p>
  
  <pre><code>package com.example.demo;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "student_tbl", schema = "user")
  public class Student {
  
  @Id
  private Integer id;
  private String name;
  private String className;
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getClassName() {
      return className;
  }
  
  public void setClassName(String className) {
      this.className = className;
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  }
  </code></pre>
  
  <p><strong>Student Repository</strong></p>
  
  <pre><code>package com.example.demo;
  
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.data.jpa.repository.Query;
  
  public interface StudentRepository extends JpaRepository&lt;Student, Integer&gt; {
  
  @Query(value = "select nextval('user.student_id')", nativeQuery = true)
  Integer createId();
  
  }
  </code></pre>
  
  <p><strong>PersistStudent</strong> <br/><br/></p>
  
  <pre><code>package com.example.demo;
  
  import java.util.List;
  import java.util.stream.IntStream;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.transaction.Transactional;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  @Service
  public class PersistStudent {
  
  @Autowired
  private StudentRepository studentRepository;
  
  @PersistenceContext
  EntityManager entityManager;
  
  @Transactional
  public void persistStudent(List&lt;Student&gt; students) {
  
      try {
  
          IntStream.range(0, students.size()).forEach(index -&gt; {
              if (students.size() == index || index % 100 == 0) {
                  entityManager.flush();
                  entityManager.clear();
              }
  
              entityManager.persist(students.get(index));
          });
      } catch (Exception e) {
          System.out.println("Exception:: " + e);
      }
  }
  
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>package com.example.demo;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class StudentContoller {
  
  @Autowired
  private PersistStudent persistStudent;
  
  @Autowired
  private StudentRepository studentRepo;
  
  @GetMapping(value = "/save")
  public void saveStudent() {
  
      List&lt;Student&gt; students = new ArrayList&lt;Student&gt;();
      Student stu = new Student();
      stu.setClassName("VII");
      stu.setName("John Doe");
      stu.setId(studentRepo.createId());
  
      students.add(stu);
  
      persistStudent.persistStudent(students);
  }
  
  }
  </code></pre>
  
  <p>Please help.</p>