2

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.

Mohan
  • 699
  • 1
  • 11
  • 27
  • You're better off using guids for identifiers. All of these problems disappear, as if by magic (to be replaced with a completely different, but simpler, set of problems) – Software Engineer Jul 12 '16 at 02:21
  • why dont you just specify initValue and allocationSize and see what happens?!! – Neil Stockton Jul 12 '16 at 07:10
  • GUIDs for object IDs are great if you love storing massive amounts of data for every primary key and foreign key value. If your company makes money selling storage solutions then go out there and tell the world to use GUIDs instead of 4 byte (Int) or 8 byte (BigInt) types for IDs. Don't forget that all these IDs end up in indexes so the table space is not only larger but every index that refers to these objects also ends up massively larger. Also tell them there's no affect on performance when accessing much more data than you have to :) That's what the cloud is for right? – Volksman Oct 31 '17 at 23:34

3 Answers3

1

sequence use only oracle, postgreSQL, DB2, H2

I know two case.

(1)

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

Auto_increment, sequence object -> strategy = GenerationType.AUTO

(2) Your case.

Element Detail

public abstract String name (Required) A unique generator name that can be referenced by one or more classes to be the generator for primary key values.

public abstract String sequenceName (Optional) The name of the database sequence object from which to obtain primary key values. Defaults to a provider-chosen value. Default: hibernate_sequence

public abstract int initialValue (Optional) The value from which the sequence object is to start generating. Default:1

public abstract int allocationSize (Optional) The amount to increment by when allocating sequence numbers from the sequence. Default:50

DDL

create sequence RTDS_ADSINPUT_SEQ start with 1 increment by 1;

Entity

@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
}
0gam
  • 1,343
  • 1
  • 9
  • 21
  • Thanks @Byeon0gam, I have taken you're solution with few more changes for having "nocache". After creating table, i have created sequence using liquibase sql tag and using that sequence name in my entity class sequenceName. Now everything works fine till now. – Mohan Jul 27 '16 at 21:40
0

As i know we do not have such annotation properties. But ideally, you should create the sequence using your own SQL query instead using the default one generated by hibernate. When the sequence existed in database, Hibernate will not generate one, you can also disable hibernate ddl generation.

Lê Thọ
  • 334
  • 1
  • 9
0

We can generate sequence id using hibernate in below mentioned manner

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int uniqueid;
Yoshita Mahajan
  • 443
  • 4
  • 6