0

Is there a way to configure or override the JPA @Version annotation to use Strings (UUID's) ?

I have a simple example of optimistic concurrency working using int as per examples

int version

@Version
Public Integer getVersion ()
    return version;
...

We have a requirement to look at using GUID's for versioning instead (at the moment held in a Postgres db as a character varying 100).

I'll be honest I've got my example working a short while ago and haven't dug around too much yet. My first attempt was a bit of a hopeful shot in the dark -

String version

@Version @GeneraedValue(generator="system-uuid")
Public String getVersion ()
    return version;
...

basically I think this is the way you use UUID for primary keys. I guess if this was going to work they would have documented this!

I'm sure I've heard it is possible though although my initial googling han't come up with anything so I thought I'd ask if anyone had done this before or could point me in the direction of a rough approach (I'm assuming it would be a bit deeper than my simple attempt) ?

1 Answers1

0

Nope, the JPA spec doesn't allow for that. Time-based or number based. And you certainly would never have a generation strategy for one, since that is for PKs, and since the version is assigned directly by the JPA provider.

From the javadoc for @Version

The following types are supported for version properties: int, Integer, short, Short, long, Long, java.sql.Timestamp.

  • But there is also a sentence n the documentation for @Version tag that says β€˜The version column can be any kind of type, as long as you define and implement the appropriate UserVersionType.' ? I've not used UserVersionType before so I'll look at that a bit more unless you know I'm on the wrong track... – Jean de Florette Jan 24 '19 at 07:43
  • That is not in the JPA spec, and neither is "UserVersionType". That is presumably Hibernate specific, non-portable. Go there and it's on your head. Since your question says "How to configure JPA" I answer for JPA. –  Jan 24 '19 at 07:53
  • fair point well made. Ill accept the answer and open a hibernate specific question. – Jean de Florette Jan 24 '19 at 11:16