0

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; }
}
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
MigRome
  • 1,095
  • 1
  • 12
  • 28
  • please add mapped class definition and sample code – Luca Basso Ricci Sep 02 '15 at 06:56
  • `@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; }` – MigRome Sep 02 '15 at 07:00
  • Those classes were generated in Eclipse via Hibernate plugin. But I think the problem is from the PGdatabase side. I just create this entity and call the method `getcurrentsession.saveorupdate(entidad)` – MigRome Sep 02 '15 at 07:02
  • follow @Viswanath D solution – Luca Basso Ricci Sep 02 '15 at 07:43

1 Answers1

1

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

@Id
@GeneratedValue(generator="Hib_Seq")
@SequenceGenerator(name="Hib_Seq", sequenceName="swq")
private int idmotiv;
Viswanath Donthi
  • 1,791
  • 1
  • 11
  • 12
  • Also, you can check this [http://www.javabeat.net/jpa-annotations-generatedvalue-sequencegenerator-tablegenerator/](link) – MigRome Sep 02 '15 at 19:56
  • Becareful in case you want to code this lines: ` @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="LICENSE_SEQ") ` With Hibernate that causes that the sequence increments from 50 to 50. As it's mentioned in this http://stackoverflow.com/questions/4288740/hibernate-use-of-postgresql-sequence-does-not-affect-sequence-table – MigRome Sep 02 '15 at 19:57