title: JPA 2 @SequenceGenerator @GeneratedValue producing unique constraint violation
body: <p><strong>Problem Overview</strong></p>
  
  <p>At seemingly random times we get an exception "postgresql duplicate key violates unique constraint." I do think I know what our problem"s" are but I don't want to make changes to the code without having a reproducible test case. But since we haven't been able to reproduce it in any environment other than randomly in production, I'm asking assistance from SO.</p>
  
  <p>In this project we have multiple postgres databases, and a primary key sequence configured for each table in each database. These sequences are created like this:</p>
  
  <pre><code>create sequence PERSONS_SEQ;
  create sequence VISITS_SEQ;
  etc...
  </code></pre>
  
  <p>We use these sequences to generate the primary keys for the entities like this:</p>
  
  <pre><code>@Entity
  @Table(name = "visits")
  public class Visit {
    @Id
    @Column(name = "id")
    @SequenceGenerator(name = "seq", sequenceName = "visits_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
    private int id;
    ...
  }
  
  @Entity
  @Table(name = "person")
  public class Person {
    @Id
    @Column(name = "id")
    @SequenceGenerator(name = "seq", sequenceName = "persons_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
    private int id;
    ...
  }
  </code></pre>
  
  <p><strong>Analysis</strong></p>
  
  <p>I think I recognize 2 problems with this configuration:</p>
  
  <p>1) Both @SequenceGenerators specify the same name attribute even though they are supposed to map to different database sequences.</p>
  
  <p>2) The @SequenceGenerator allocationSize attribute defaults to 50 (we're using hibernate as the JPA provider) so I think the create sequence syntax should specify how much the sequence should increment by, specifically by 50 to match the allocationSize.</p>
  
  <p>Based on this guess, I think the code should be modified to something like this:</p>
  
  <pre><code>create sequence PERSONS_SEQ increment by 50;
  create sequence VISITS_SEQ increment by 50;
  etc...
  
  @Entity
  @Table(name = "visits")
  public class Visit {
    @Id
    @Column(name = "id")
    @SequenceGenerator(name = "visits_seq", sequenceName = "visits_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "visits_seq")
    private int id;
    ...
  }
  
  @Entity
  @Table(name = "person")
  public class Person {
    @Id
    @Column(name = "id")
    @SequenceGenerator(name = "persons_seq", sequenceName = "persons_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "persons_seq")
    private int id;
    ...
  }
  </code></pre>
  
  <p>I would just test this rather than asking the question on SO, but again, we have not been able to reproduce this production issue in any other environments. And even in production the unique constraint violation only occurs at seemingly random times.</p>
  
  <p><strong>Questions:</strong></p>
  
  <p>1) Am I correct in my analysis of what the changes should be to fix this unique constraint violation?</p>
  
  <p>2) What are the best practices for using sequence generators when using hibernate as a JPA provider? </p>