Skip to content
Advertisement

JPA : Re-use Id generated from a sequence for a new version of the same object having composite PK (ID + VERSION)

I have entity A with composite PK [ id(generated from sequence) + version ].

For a brand new record I want to pick the id from a sequence defined in the DB side. Lets say its created like below

JavaScript

Next time, I want a new version of the same Id to be created like below

JavaScript

Note : in the second case I don’t want it to be generated by the sequence generator, coz I want to manually provide it.

Is it possible in JPA/Hibernate ? If possible could someone please tell how to do it ?

Many thanks in advance!

Advertisement

Answer

Hibernate ORM doesn’t support the generation of id with composite keys.

You can probably run a native SQL when you create a new object. With PostgreSQL for example:

JavaScript

Where EmebeddedId is the composite key of your entity:

JavaScript

Where mysequence is the name of a sequence on the database.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement