0 Replies Latest reply on Jan 3, 2007 11:48 AM by Richard Teviotdale

    Mapping entity primary key to Postgresql serial sequence

    Richard Teviotdale Newbie

      While testing an entity bean mapped to a Postgresql database table I recieved some "duplicate key violates unique constraint" errors. The errors occured during concurrant access to the database by jboss and psql using different database roles. After setting the SequenceGenerator annotation attribute allocationSize = 1 this issue was resolved.

      // database table
      CREATE TABLE people (
       id SERIAL PRIMARY KEY
       , name VARCHAR(32)
      );
      // CREATE TABLE will create implicit sequence "people_id_seq" for serial column "people.id"
      
      // entity bean
      @Entity
      @Table(name = "people", schema = "public")
      public class People implements Serializable
      {
       private static final long serialVersionUID = 1234L;
       private int id;
       private String name;
      
       public People() {}
      
       @Id
       @SequenceGenerator(name = "identifier", sequenceName = "people_id_seq", initialValue = 1, allocationSize = 1)
       @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "identifier")
       @NotNull
       public int getId()
       {
       return id;
       }
      
       public void setId(int id)
       {
       this.id = id;
       }
      
       @Column(name = "name", length = 32)
       @Length(max = 32)
       public String getName()
       {
       return this.name;
       }
      
       public void setName(String name)
       {
       this.name = name;
       }
      }