Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres + Hibernate + Java UUID

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?

like image 229
mainstringargs Avatar asked Apr 17 '09 14:04

mainstringargs


People also ask

Does PostgreSQL support UUID?

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.

Can I use hibernate with PostgreSQL?

Out of the box, Hibernate works pretty well with PostgreSQL databases.

What is Postgres UUID?

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.

How does Postgres connect to hibernate?

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.


2 Answers

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.

like image 107
Stephen Denne Avatar answered Oct 08 '22 10:10

Stephen Denne


Try this

@Column(name = "UUID", nullable=false, insertable = false, columnDefinition="uuid DEFAULT uuid_generate_v4()")
private String uuid;
like image 33
Rajesh Guptan Avatar answered Oct 08 '22 12:10

Rajesh Guptan