Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto creation of sequence using hibernate tool

I wanted to 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

INCREMENT BY,NOCACHE CYCLE

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  ORDER  CYCLE ;

But as far I saw hibernate only support name, sequncename,allocation,initialvalue

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

like image 891
Satheesh Cheveri Avatar asked Mar 01 '13 09:03

Satheesh Cheveri


People also ask

What is Hibernate sequence generator?

SEQUENCE is the generation type recommended by the Hibernate documentation. The generated values are unique per sequence. If we don't specify a sequence name, Hibernate will reuse the same hibernate_sequence for different types.

How does Hibernate sequence work?

IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key, SEQUENCE: Hibernate requests the primary key value from a database sequence, TABLE: Hibernate uses a database table to simulate a sequence.

What is GenerationType Auto?

GenerationType.AUTO sets @GeneratedValue automatic. If table has defined any default value or it has defined any auto increment in table then in that case we use. @GeneratedValue(strategy=GenerationType. AUTO)

What is the use of @GeneratedValue in Hibernate?

Provides for the specification of generation strategies for the values of primary keys. The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation.


1 Answers

I looked it up in the Hibernate sources (4.2.7). It is not possible to specify this with an annotation (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
    }
}

Have an entity like this

@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 set your new Dialect as described in the Hibernate doc (http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-optional-dialects)

like image 113
tine2k Avatar answered Sep 28 '22 06:09

tine2k