How to use existing Oracle sequence to generate id in hibernate? How to use existing Oracle sequence to generate id in hibernate? oracle oracle

How to use existing Oracle sequence to generate id in hibernate?


The answer to the original question:

@SequenceGenerator(name="EL_SEQ", sequenceName="EL_SEQ",allocationSize=1)

It is allocationSize that sets the value to increment by.


I'm not used to use annotations, this is what I have in my *.hbm.xml:

<id name="id" type="java.lang.Integer">    <column name="ID_PRODUCT" />    <generator class="sequence-identity" >        <param name="sequence">PRODUCT_ID_SEQ</param>    </generator></id>

You can easily map this to annotations. The generator sequence-identity uses auto increment with sequences.


Here is a working example with annotations, this way, the existing DB sequence will be used (you can also use the "sequence" strategy, but with less performance on insertion) :

@Entity@Table(name = "USER")public class User {    // (...)    @GenericGenerator(name = "generator", strategy = "sequence-identity", parameters = @Parameter(name = "sequence", value = "USER_SEQ"))    @Id    @GeneratedValue(generator = "generator")    @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)    public Long getId() {        return this.id;    }