Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JPA be persuaded to convert between eg UUIDs and Strings?

I have a Java object with a field which is a UUID. I'd like to be able to persist this object to the database in the obvious way; however, the Basic mapping will use Java serialization to write it, while I want the UUID to be present in its obvious string form. Is there a way to supply a UUID <-> String converter to JPA for that field which will be used at read and write time so I can handle this type naturally?

like image 694
Paul Crowley Avatar asked Dec 09 '11 13:12

Paul Crowley


People also ask

Can we convert UUID to string?

Using the str() function to convert UUID to String in Python The str() function is used to typecast different objects to a string. We can use this function to convert UUID to String in Python. In the above example, We create a uuid object using the uuid1() function.


1 Answers

JPA 2.0 doesn't provide a general way to do it, except for creating separate getters/setters for different representations of the same field.

Depending on your JPA provider you can use implementation-specific methods, for example, Hibernate provides a uuid-char type for this purpose:

@Type(type = "uuid-char")
private UUID uuid;
like image 158
axtavt Avatar answered Oct 18 '22 12:10

axtavt