MigRome
MigRome

Reputation: 1155

Hibernate+Postgresql: PK not starting from 1 (PG sequence created starting from 1)

I have a table with a primary key of SERIAL type. This primary key is affected by a sequence like this:

CREATE SEQUENCE swq
INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807
START 1 CACHE 1;

My question is: why the primary key, after a first execution of the saveorUpdate() method it creates the first record, but inserting the id field as ZERO (0) instead of ONE(1)?


From a comment by the OP:

@Entity @Table(name = "table", schema = "schem") 
public class tableBE implements java.io.Serializable 
{ 
  private int idmotiv; 

  @Id @Column(name = "_id", unique = true, nullable = false) 
  public int getIdmot() { return this.idmot; }
}

Upvotes: 0

Views: 585

Answers (1)

Viswanath Donthi
Viswanath Donthi

Reputation: 1821

Use @GeneratedValue & @SequenceGenerator annotation configuration. Below is the example.

@Id
@GeneratedValue(generator="Hib_Seq")
@SequenceGenerator(name="Hib_Seq", sequenceName="swq")
private int idmotiv;

Upvotes: 1

Related Questions