Automatically create a sequence using the sleep tool

I wanted to create a sequence using the hibernate tool (pojo to sql). And it definitely works great.

@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 that I want to specify properties such as

 INCREMENT BY,NOCACHE CYCLE 

and the final ddl script should be something like below

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

But so far I have seen that sleep mode only supports name, sequncename,allocation,initialvalue

I ask for advice if I can include these properties as an annotation in pojo.

+8
java hibernate sequence
source share
2 answers

I looked at the sources of sleep mode (4.2.7). This cannot be specified using annotations (neither JPA nor Hibernate).

However, you can provide your own Dialect to achieve this.

 public class MyOwnOracleDialect extends Oracle10gDialect { @Override protected String getCreateSequenceString(final String sequenceName, final int initialValue, final int incrementSize) throws MappingException { String createSequenceString = super.getCreateSequenceString(sequenceName, initialValue, incrementSize); return createSequenceString + " NOCACHE ORDER CYCLE"; // modify this string as you like } } 

Do you have such an object

 @Entity public class MyEntity { @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen") @SequenceGenerator(name = "seqid-gen", sequenceName = "RTDS_ADSINPUT_SEQ", allocationSize = 1, initialValue = 0) @Column(name="id") private Long id; // ... } 

You can install a new dialect as described in the Hibernate document ( http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-optional-dialects )

+7
source share

I think you are looking for something like this

  <id name="pk_field" column="column_name"> <generator class="sequence"> <param name="sequence">sequence_name</param> <param name="parameters">START WITH 5 INCREMENT BY 10</param> </generator> </id> 
0
source share

All Articles