【问题标题】:Hibernate+Postgresql: PK not starting from 1 (PG sequence created starting from 1)Hibernate+Postgresql:PK 不是从 1 开始(从 1 开始创建的 PG 序列)
【发布时间】:2015-11-27 13:22:03
【问题描述】:

我有一个主键为 SERIAL 类型的表。 此主键受以下序列影响:

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

我的问题是:为什么主键在第一次执行 saveorUpdate() 方法后会创建第一条记录,但将 id 字段插入为零 (0) 而不是 ONE(1)?


来自 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; }
}

【问题讨论】:

  • 请添加映射类定义和示例代码
  • @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; }
  • 这些类是在 Eclipse 中通过 Hibernate 插件生成的。但我认为问题出在 PGdatabase 方面。我只是创建这个实体并调用方法getcurrentsession.saveorupdate(entidad)
  • 关注@Viswanath D 解决方案

标签: database hibernate postgresql sequence


【解决方案1】:

使用@GeneratedValue & @SequenceGenerator 注解配置。下面是例子。

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

【讨论】:

  • 另外,你可以查看这个 [javabeat.net/…
  • 要小心,以防您想编写以下代码行:`@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="LICENSE_SEQ")` 使用 Hibernate 会导致序列从 50 增加到 50。因为它是在这个stackoverflow.com/questions/4288740/…中提到
猜你喜欢
  • 2013-03-04
  • 2016-06-01
  • 1970-01-01
  • 2019-09-20
  • 2014-07-05
  • 2015-06-25
  • 2019-10-15
  • 1970-01-01
  • 2017-02-06
相关资源
最近更新 更多