I want to use PostgreSQL's native UUID type with a Java UUID. I am using Hibernate as my JPA provider and ORM. If I try to save it directly, it is just saved as a bytea in Postgres.
How can I do this?
UUID is an abbreviation for Universal Unique Identifier defined by RFC 4122 and has a size of 128-bit. It is created using internal algorithms that always generate a unique value. PostgreSQL has its own UUID data type and provides modules to generate them.
Out of the box, Hibernate works pretty well with PostgreSQL databases.
What is the Postgres UUID Type? The Postgres UUID data type stores UUIDs as defined by RFC 4122, which are 128-bit quantities generated by algorithms that minimize the probability of having duplicate identifiers. A UUID comprises of 32 hexadecimal digits, represented in groups of 8,4,4,4 and 12.
Connect Hibernate to PostgreSQL DataSwitch to the Hibernate Configurations perspective: Window -> Open Perspective -> Hibernate. Right-click on the Hibernate Configurations panel and click Add Configuration. Set the Hibernate version to 5.2. Click the Browse button and select the project.
Try use the latest development version of the JDBC driver (Currently 8.4dev-700), or wait for the next release version. (Edited to add: 8.4-701 has been released)
The release notes mention this change:
Map the database uuid type to java.util.UUID. This only works for relatively new server (8.3) and JDK (1.5) versions.
Try this
@Column(name = "UUID", nullable=false, insertable = false, columnDefinition="uuid DEFAULT uuid_generate_v4()")
private String uuid;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With