I have Oracle table with PK field wich is DB tabele trigger generated on insert.
Trigger have its own ID generating logic. Even if that trigger use DB sequence, I want trigger to generate Id, not hibernate or OpenXava in any way. If annotate sequenc like
@SequenceGenerator(name="pk_seq",sequenceName="i3_seq",allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="pk_seq")

DB trigger generates NEXT_VAL no matter, so I have skipping sequence (2,4,6 ...)
How do I define (annotate) that field in model in a way that I leave complete control to the database?

@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

GenerationType.IDENTITY means DB is in control of the value in this column, but can be used for your case as long as your are creating the table and triggers by hand.

Another way is create a stored procedure/function to calculate the id... and call it inside your entity class by a @PrePersist method.


Last edit: jmdiazlr 2020-06-22 I tried @GeneratedValue(strategy= GenerationType.IDENTITY)
but get
"Impossible to execute Save action: org.hibernate.exception.GenericJDBCException: could not prepare statement "

(attachment errorstack.TXT)

I also tried this

@SequenceGenerator(name="pk_seq",sequenceName="i3_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="pk_seq")

and this works but only if you change DB trigger which allows the value of the ID to be entered by the client, which is not according to the business rule.

What am I doing wrong?

This is my syntax:

@Hidden
@ReadOnly
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id", length=32)
private BigInteger id;

public BigInteger getId() {
return id;

public void setId(BigInteger id) {
this.id = id;

I tried with long data type, but for real Oracle DB PK field is of type NUMBER(32)
so I need to use BigInteger, but I doubt type is the problem, bicouse as I mention, I tried long type with the same error.


Last edit: Dusan Djuric 2020-06-23

Does it work?

Also, have a look at these StackOverflow threads:
https://stackoverflow.com/questions/45670510/jpa-auto-increment-id-with-oracle-database
https://stackoverflow.com/questions/20603638/what-is-the-use-of-annotations-id-and-generatedvaluestrategy-generationtype

Help others in this forum as I help you.

This is a small test with postgresql, creating the table by hand:

CREATE table prueba (
  id varchar not null default uuid_generate_v1() primary key,
  nombre varchar not null);
@Entity
@Table(name="prueba")
@Data
public class Prueba {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;
    @Required
    private String nombre;

This works well, even if adding @Hidden to id field.

Try this small example, if not works for you maybe some problem with Oracle driver or hibernate's oracle adapter.

For a trigger generated value, I added:

CREATE OR REPLACE FUNCTION ftrg_prueba()
RETURNS trigger LANGUAGE plpgsql AS
BEGIN
  NEW.id :=  uuid_generate_v1() || 'test';
  RETURN NEW;
END;
CREATE TRIGGER trg_prueba BEFORE INSERT ON prueba
FOR EACH ROW EXECUTE FUNCTION  ftrg_prueba();

Hope this helps.

 
Last edit: jmdiazlr 2020-06-25 private BigInteger id;

and get error message
Impossible to execute Save action: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.istratech.mishzmp.model.sJezici
what I basically expected to get

I also tried AUTO with sequence name

@SequenceGenerator(name="pk_seq",sequenceName="i3_seq") 
@GeneratedValue(strategy = GenerationType.AUTO,generator="pk_seq") 

but in that case ID is generate by hibernate, not by DB trigger (btw as I mentioned earlier, I had to change DB trigger for this to work).

So ...

@GeneratedValue(strategy = GenerationType.IDENTITY)

I get
Impossible to execute Save action: org.hibernate.exception.GenericJDBCException: could not prepare statement
and this in trase

WARN: SQL Error: 17068, SQLState: 99999
lip 26, 2020 1:14:28 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Invalid argument(s) in call
lip 26, 2020 1:14:28 PM org.openxava.model.impl.MapFacadeBean create
SEVERE: org.hibernate.exception.GenericJDBCException: could not prepare statement
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement

Analyzing trace took me to
https://support.oracle.com/knowledge/Middleware/1234235_1.html

After downloading this JDBC driver
https://download.oracle.com/otn-pub/otn_software/jdbc/197/ojdbc8-full.tar.gz
and defining lib link to ojdbc8.jar and orai18n.jar (in my case) things finally worked out well!

Story with Oracle JDBC driver names is confusing because old driver was also ojdbc8-full
and was downloaded from
https://www.oracle.com/database/technologies/jdbc-ucp-122-downloads.html#license-lightbox

 
Last edit: Dusan Djuric 2020-06-26