1

I'm adding JPA (Hibernate) to a project that uses uuids in Postgresql. The uuids are the Postgresql uuid type.

I've followed these instructions and added Hibernate annotations like this:

@Column(name = "uid")
@Type(type="pg-uuid")
private UUID uid;

Hibernate supposedly added support for Postgres UUIDs in Hibernate 3.6. I've tried Hibernate 3.6 and 4.0 but both throw the Exception below.

The Exception shows that the Postgresql driver is trying to convert the uuid to a long. I've tried the 9.0-801.jdbc4 and 9.1-901.jdbc4 Postgres drivers. Do I need to get a different database driver?

Caused by: org.postgresql.util.PSQLException: Bad value for type long : dbada1e4-a165-4601-b34e-d08428cd81df
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toLong(AbstractJdbc2ResultSet.java:2796)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2019)
    at org.postgresql.jdbc4.Jdbc4ResultSet.getBlob(Jdbc4ResultSet.java:52)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBlob(AbstractJdbc2ResultSet.java:335)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:104)
    ... 32 more
Community
  • 1
  • 1
Dean Schulze
  • 9,633
  • 24
  • 100
  • 165
  • 1
    That mapping looks correct. Can you show the rest of the stack trace? – Steve Ebersole Jun 06 '12 at 12:55
  • The psql-bugs team responded and said that this is a Hibernate mapping problem, not a problem with the Postgresql driver. In other modules we use straight JDBC calls and return the UUID from the result set as a String and it works. The psql-bugs team said that I need to convince Hibernate to get the UUID from the result set as a String. That's consistent with what others have seen. So it looks like a Hibernate issue. – Dean Schulze Jun 06 '12 at 13:48
  • I also tried OpenJPA and it can't handle UUIDs either. It throws an Exception complaining about "invalid stream header: 64626164". Postgresql stores UUID characters in 4-bytes rather than 8 and Postgresql UUIDs are not ANSI compliant. This may be an issue with all JPA implementations. – Dean Schulze Jun 06 '12 at 13:56
  • Never said it was or wasn't a Hibernate bug. I just asked for the rest of the stack trace to see what is going on. – Steve Ebersole Jun 06 '12 at 13:59
  • And no, the bigger problem with Postgres is its JDBC driver in general. It is a second class citizen to them. Here they decided to map their custom UUID type to the JDBC type OTHER instead of a custom code which would have made more sense. And to compound matters they map multiple custom types to OTHER. Anyway, I need to see the rest of the stack trace to see what is going on – Steve Ebersole Jun 06 '12 at 14:01
  • Sorry, we've already changed the Postgres UUID types in the database to varchar(36) so I can't reproduce the full stack trace. Is there a better JDBC driver available? – Dean Schulze Jun 06 '12 at 16:13
  • I know of no other JDBC drivers for PostgreSQL – Steve Ebersole Jun 06 '12 at 16:41

0 Answers0