Postgres JDBC and Hibernate Using UUID Types

While working with Postgres UUID type and Java, if we assign the value of type UUID as a string then we will get the following exception.

org.postgresql.util.PSQLException: ERROR: column "aggregateid" is of type uuid but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.

To assigning value incorrect way is given below, if you are using PreparedStatement.

If the value of UUID is a string type, then first you need to convert it to Java UUID, then set parameter value as an object in PreparedStatement.

stmt.setObject(1, UUID.fromString(uuidStr));

In the case of hibernate define your entity as given below.

@Type(type = "pg-uuid")
@Column(name = "id", columnDefinition = "uuid")
private UUID id;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.