creating sequence Id in hibernate

时间:2016-07-11 21:41:41

标签: java hibernate jpa primary-key sequence

I wanted to generate sequence using hibernate tool ( pojo to sql). And definitely it works fine.

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen")
@SequenceGenerator(name = "seqid-gen", sequenceName = "RTDS_ADSINPUT_SEQ" )
@Column(name="id")  
public Long getId() {  
    return id;  
} 

This code generates below sql

create sequence RTDS_ADSINPUT_SEQ;  

The problem is I wanted to specify properties like

START WITH, INCREMENT BY, NOCACHE

and the final ddl script should be some thing like below

CREATE SEQUENCE  RTDS_ADSINPUT_SEQ  MINVALUE 1 MAXVALUE
999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE;

But as far I saw hibernate only support name, sequncename, allocationSize, initialvalue. My doubt is, if we use allocationSize = 1 & initialValue = 1 do we still need to mention "nocache". If so, do we have any kind of annotation for "nocache"?

Please advice me if I can include that properties as annotation in the pojo.

3 个答案:

答案 0 :(得分:0)

据我所知,我们没有这样的注释属性。但理想情况下,您应该使用自己的SQL查询创建序列,而不是使用hibernate生成的默认序列。当序列存在于数据库中时,Hibernate不会生成一个,也可以禁用hibernate ddl生成。

答案 1 :(得分:0)

序列仅使用oracle,postgreSQL,DB2,H2

我知道两个案例。

(1)

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int idx;

Auto_increment,序列对象 - > strategy = GenerationType.AUTO

(2) 你的情况。

  
    

元素明细

  
     

public abstract String name (必需)唯一的生成器名称   可以被一个或多个类引用为生成器   主键值。

     

public abstract String sequenceName (可选)的名称   数据库序列对象,从中获取主键值。   默认为提供者选择的值。默认值:hibernate_sequence

     

public abstract int initialValue (可选)从中获取的值   sequence对象是开始生成的。默认值:1

     

public abstract int allocationSize (可选)要增加的金额   当从序列中分配序列号时。默认值:50

DDL

create sequence RTDS_ADSINPUT_SEQ start with 1 increment by 1;

实体

@Entity
@SequenceGenerator(
name = "seqid-gen", 
sequenceName = "RTDS_ADSINPUT_SEQ" 
initiaValue = 1, allocationSize = 1)
public class XXX {

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen")
   private long id;

    //getter, setter
}

答案 2 :(得分:0)

我们可以通过下面提到的方式使用hibernate生成序列id

subprocess.call(['ifconfig', interface, 'down'], shell=True)
subprocess.call(['ifconfig', interface, 'hw', 'ether', address], shell=True)
subprocess.call(['ifconfig', interface, 'up'], shell=True)