title: Hibernate doesn't use PostgreSQL sequence to generate primary key
body: <p>I have a Java Server Faces project using Hibernate to store data into a PostgreSQL database.</p>
  
  <p>The problem I'm having is that Hibernate doesn't take the existing sequence (fifo_id_seq) into consideration while saving the data into the db; it saves with the id = 0;</p>
  
  <p>The answer may be very obvious, as I'm new to Hibernate.</p>
  
  <p>I'm sorry for the tons of code, but I wanted to post everything that may be relevant.</p>
  
  <p>The fifo table:</p>
  
  <pre><code>                                    Table "public.fifo"
         Column        |       Type       |                     Modifiers                     
  ---------------------+------------------+---------------------------------------------------
   id                  | integer          | not null default nextval('fifo_id_seq'::regclass)
   number_of_processes | integer          | 
   total_time          | integer          | 
   average_time        | double precision | 
   average_wait_time   | double precision | 
   total_wait_time     | integer          | 
  Indexes:
  "fifo_pkey" PRIMARY KEY, btree (id)
  </code></pre>
  
  <p>The fifo_id_seq sequence:</p>
  
  <pre><code>         Sequence "public.fifo_id_seq"
      Column     |  Type   |        Value        
  ---------------+---------+---------------------
   sequence_name | name    | fifo_id_seq
   last_value    | bigint  | 1
   start_value   | bigint  | 1
   increment_by  | bigint  | 1
   max_value     | bigint  | 9223372036854775807
   min_value     | bigint  | 1
   cache_value   | bigint  | 1
   log_cnt       | bigint  | 0
   is_cycled     | boolean | f
   is_called     | boolean | f
  </code></pre>
  
  <p>FifoEntity.java:
  *note: constructors and annotations for @Id added by me, the rest is generated</p>
  
  <pre><code>@Entity
  @Table(name = "fifo", schema = "public", catalog = "processmanagement")
  public class FifoEntity {
  private int id;
  private Integer numberOfProcesses;
  private Integer totalTime;
  private Double averageTime;
  private Double averageWaitTime;
  private Integer totalWaitTime;
  
  public FifoEntity()
  {
      // empty
  }
  
  public FifoEntity(int numberOfProcesses, int totalTime, double averageTime, double averageWaitTime, int totalWaitTime)
  {
      this.numberOfProcesses = numberOfProcesses;
      this.totalTime = totalTime;
      this.averageTime = averageTime;
      this.averageWaitTime = averageWaitTime;
      this.totalWaitTime = totalWaitTime;
  }
  
  @Id
  @SequenceGenerator(name="fifo_pkey", sequenceName="fifo_id_seq", allocationSize = 1)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="fifo_pkey")
  @Column(name = "id", unique = true, nullable = false)
  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  @Basic
  @Column(name = "number_of_processes")
  public Integer getNumberOfProcesses() {
      return numberOfProcesses;
  }
  
  public void setNumberOfProcesses(Integer numberOfProcesses) {
      this.numberOfProcesses = numberOfProcesses;
  }
  
  @Basic
  @Column(name = "total_time")
  public Integer getTotalTime() {
      return totalTime;
  }
  
  public void setTotalTime(Integer totalTime) {
      this.totalTime = totalTime;
  }
  
  @Basic
  @Column(name = "average_time")
  public Double getAverageTime() {
      return averageTime;
  }
  
  public void setAverageTime(Double averageTime) {
      this.averageTime = averageTime;
  }
  
  @Basic
  @Column(name = "average_wait_time")
  public Double getAverageWaitTime() {
      return averageWaitTime;
  }
  
  public void setAverageWaitTime(Double averageWaitTime) {
      this.averageWaitTime = averageWaitTime;
  }
  
  @Basic
  @Column(name = "total_wait_time")
  public Integer getTotalWaitTime() {
      return totalWaitTime;
  }
  
  public void setTotalWaitTime(Integer totalWaitTime) {
      this.totalWaitTime = totalWaitTime;
  }
  
  @Override
  public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
  
      FifoEntity that = (FifoEntity) o;
  
      if (id != that.id) return false;
      if (averageTime != null ? !averageTime.equals(that.averageTime) : that.averageTime != null) return false;
      if (averageWaitTime != null ? !averageWaitTime.equals(that.averageWaitTime) : that.averageWaitTime != null)
          return false;
      if (numberOfProcesses != null ? !numberOfProcesses.equals(that.numberOfProcesses) : that.numberOfProcesses != null)
          return false;
      if (totalTime != null ? !totalTime.equals(that.totalTime) : that.totalTime != null) return false;
      if (totalWaitTime != null ? !totalWaitTime.equals(that.totalWaitTime) : that.totalWaitTime != null)
          return false;
  
      return true;
  }
  
  @Override
  public int hashCode() {
      int result = id;
      result = 31 * result + (numberOfProcesses != null ? numberOfProcesses.hashCode() : 0);
      result = 31 * result + (totalTime != null ? totalTime.hashCode() : 0);
      result = 31 * result + (averageTime != null ? averageTime.hashCode() : 0);
      result = 31 * result + (averageWaitTime != null ? averageWaitTime.hashCode() : 0);
      result = 31 * result + (totalWaitTime != null ? totalWaitTime.hashCode() : 0);
      return result;
  }
  }
  </code></pre>
  
  <p>FifoEntity mapped in hbm.xml:
  *</p>
  
  <pre><code>&lt;class name="com.processmanagement.hibernate.entities.FifoEntity" table="fifo" schema="public" catalog="processmanagement"&gt;
      &lt;id name="id" column="id"&gt;
          &lt;generator class="sequence-identity"&gt;
              &lt;param name="sequence"&gt;fifo_id_seq&lt;/param&gt;
          &lt;/generator&gt;
      &lt;/id&gt;
      &lt;property name="numberOfProcesses" column="number_of_processes"/&gt;
      &lt;property name="totalTime" column="total_time"/&gt;
      &lt;property name="averageTime" column="average_time"/&gt;
      &lt;property name="averageWaitTime" column="average_wait_time"/&gt;
      &lt;property name="totalWaitTime" column="total_wait_time"/&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>And this is how I'm trying to save the data into the db using Hibernate:</p>
  
  <pre><code>    FifoEntity fifoEntity = new FifoEntity(processList.size(), totalTime,
              averageProcessTime, averageWaitTime, totalWaitTime);
  
      databaseHelper.saveOutcomeToDatabase(fifoEntity);
  .
  .
  .
      public void saveOutcomeToDatabase(Object object)
  {
  //        session.beginTransaction();
  //        session.save(object);
  //        session.getTransaction().commit();
      Transaction transaction = session.beginTransaction();
      session.persist(object);
      transaction.commit();
      session.clear();
  }
  </code></pre>
  
  <p>Of course, the error when trying to save a consecutive object:</p>
  
  <pre><code>org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "fifo_pkey"
    Detail: Key (id)=(0) already exists.
  </code></pre>