我怎样才能强制自动生成的hibernate主键 [英] How can I force positive auto generated hibernate primary keys

查看:119
本文介绍了我怎样才能强制自动生成的hibernate主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Hibernate 4.3.8的JPA 2.1。在Oracle,MySQL,PostgreSQL和MS SQL上。为了创建数字主键,我使用了以下代码:

  @Id 
@Column(name =ID )
@GeneratedValue(strategy = GenerationType.AUTO,generator =t_gen)
@SequenceGenerator(name =t_gen,sequenceName =T_SEQ)
private long id;

到目前为止这么好。但是我需要在所有条件下仅生成ID的正值。如何强制Hibernate在每个数据库(MS SQL,MySQL,PostgreSQL,Oracle)上只生成正面的ID?解析方案

这有助于;



如果未在实体端声明allocationSize,则默认分配大小为50个所有序列必须在DB端声明INCREMENT BY 50。



参考链接:
http://skay-dev.blogspot.in/2013/09/hibernate-sequence-and-negative.html


I am using JPA 2.1 with Hibernate 4.3.8. on Oracle, MySQL, PostgreSQL and MS SQL. For creating numeric primary keys I have used the following code :

  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "t_gen")
  @SequenceGenerator(name = "t_gen", sequenceName = "T_SEQ")
  private long id;

So far so good. But I need to generate only positive values of IDs under all conditions. How can I force Hibernate to generate only positive ID's on each database (MS SQL, MySQL, PostgreSQL, Oracle) ?

解决方案

Hope this helps;

If allocationSize is not declared on the entity side, as default allocation size is 50 ALL sequences MUST declare INCREMENT BY 50 on the DB side.

Reference Link: http://skay-dev.blogspot.in/2013/09/hibernate-sequence-and-negative.html

这篇关于我怎样才能强制自动生成的hibernate主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆