For my project i need to use hibernate to insert some values into a table. To do this, i absolutely need to use two sequences from the oracle database. The first one is used to generate the primary key and it’s working great. But the second is used to increment a field that is not an id.
It seems that i can’t use the @GeneratedValue annotation on a field that is not an id but i wanted to know if it was still possible to use the oracle sequence ?
Before 12c, I think you need a db trigger or init the field manually with JDBC call to get next sequence value,
which means reporting the init operation of the @Entity to the repository layer instead of the domain one.
I tried your suggestion but it seems the sequence is not called and hibernate try to insert the Default value instead. Maybe, i did something wrong, does this implementation seems correct to you ?
@Column(name="FIELD", columnDefinition = " NUMBER(19,0) DEFAULT ON NULL sensor_seq.nextval", insertable = false)
@Generated(GenerationTime.INSERT)
private long field;
Java side is not enough, did you modify the table definition in ORACLE?
BTW, it should also be possible to have a ORACLE virtual column of which expression call a function doing the nextval (you can’t have a sequence.nextval directly in the defining expression of a virual column).
The only price to pay is that you could not partition the table on that column then.
Something like: NUMBER(19,0) generated always as (mynextnsqfunc()) virtual
What I meant was “did you run the ALTER TABLE... to modify the column definition” but since your DB is 11g the DEFAULT sensor_seq.nextval will not work then better use a DB trigger or if your Hibernate version supports it (>= 4.3) the database-generated columns referenced by Vlad.
For the second option, you will have a JDBC round trip hidden in the @ValueGenerationType because you can’t just return sensor_nsq.nextval from getDatabaseGeneratedReferencedColumnValue, the generated SQL will trigger a “sequence number not allowed here” SQL error.