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
ID VERSION
1 0
Next time, I want a new version of the same Id to be created like below
ID VERSION
1 0
1 1
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!
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:
Long id = (Long) em.createNativeQuery("SELECT nextval('mysequence')").getSingleResult();
Long version = ...;
EmbeddedId id = new EmbeddedId(id, version);
Where EmebeddedId is the composite key of your entity:
@Entity
class Example {
@Id
EmbeddedId id;
...
}
@Embeddable
class EventId implements Serializable {
Long id;
Long version;
...
}
Where mysequence
is the name of a sequence on the database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.