javajpaoptimistic-concurrency

Can you configure JPA to use a UUID for @Version in optimistic concurrency


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) ?


Solution

  • 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.