Postgres UUID JDBC not working
Asked Answered
P

4

21

The latest Java JDBC drivers for postgres claim to support UUIDs natively; working against Postgres 9.2 (mac).

Indeed, when a PreparedStatement is used, I can step through the driver code, and even walk through the specialised 'setUuid' function in AbstractJdbc3gStatement.java. By all indications, it should 'just work'.

However, it does not work. The database flings back an error, which I receive thus:

Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = bytea
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 139
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157) ~[postgresql-9.2-1002.jdbc4.jar:na]

Yes, indeed, setUuid in the JDBC driver does send that as a bytea :

private void setUuid(int parameterIndex, UUID uuid) throws SQLException {
        if (connection.binaryTransferSend(Oid.UUID)) {
            byte[] val = new byte[16];
            ByteConverter.int8(val, 0, uuid.getMostSignificantBits());
            ByteConverter.int8(val, 8, uuid.getLeastSignificantBits());
            bindBytes(parameterIndex, val, Oid.UUID);
        } else {
            bindLiteral(parameterIndex, uuid.toString(), Oid.UUID);
        }
    }

What gives? Is there some magic rune required in the actual database to bless this conversion ?

Pizzicato answered 31/7, 2013 at 11:43 Comment(1)
Can you post the actual PreparedStatement you are trying to run?Jovian
B
38

tl;dr

myPreparedStatement.setObject( 
    … , 
    java.util.UUID.randomUUID()
)

Details

(a) Show us your code.

PreparedStatement::setObject does work when passing a java.util.UUID. You likely have some other issue in your code.

(b) See my blog post UUID Values From JDBC to Postgres for a bit of discussion and example code.

// Generate or obtain data to store in database.
java.util.UUID uuid = java.util.UUID.randomUUID(); // Generate a random UUID. 
String foodName = "Croissant";
// JDBC Prepared Statement.
PreparedStatement preparedStatement = conn.prepareStatement( "INSERT INTO food_ (pkey_, food_name_  ) VALUES (?,?)" );
int nthPlaceholder = 1; // 1-based counting (not an index).
preparedStatement.setObject( nthPlaceholder++, uuid ); 
preparedStatement.setString( nthPlaceholder++, foodName ); 
// Execute SQL.
if ( !( preparedStatement.executeUpdate() == 1 ) ) { 
  // If the SQL reports other than one row inserted…
  this.logger.error( "Failed to insert row into database." );
}

(c) I'm not sure what you mean by

The latest Java JDBC drivers for postgres claim to support UUIDs natively

Which driver? There are at least two open-source JDBC drivers for Postgres, the current/legacy one and a new rewrite "next generation" one. And there are other commercial drivers as well.

"natively"? Can you link to the documentation you read? The SQL spec has no data type for UUID (unfortunately ☹), therefore the JDBC spec has no data type for UUID. As a workaround, the JDBC driver for Postgres uses the setObject and getObject methods on PreparedStatement move the UUID across the chasm between Java ↔ SQL ↔ Postgres. See the example code above.

As the PreparedStatement JDBC doc says:

If arbitrary parameter type conversions are required, the method setObject should be used with a target SQL type.

Perhaps by "natively", you confused Postgres' native support for UUID as a data type with JDBC having a UUID data type. Postgres does indeed support UUID as a data type, which means the value is stored as 128-bits rather than multiple times that if it were stored as as ASCII or Unicode hex string. And being native also means Postgres knows how to build an index on a column of that type.

The point of my blog post mentioned above was that I was pleasantly surprised by how simple it is to bridge that chasm between Java ↔ SQL ↔ Postgres. In my first uneducated attempts, I was working too hard.


Another note about Postgres supporting UUID… Postgres knows how to store, index, and retrieve existing UUID values. To generate UUID values, you must enable the Postgres extension (plugin) uuid-ossp. This extension wraps a library provided by The OSSP Project for generating a variety of kinds of UUID values. See my blog for instructions.


By the way…

If I knew how to petition the JDBC expert group or JSR team to make JDBC aware of UUID, I certainly would. They are doing just that for the new date-time types being defined in JSR 310: Date and Time API.

Similarly, if I knew how to petition the SQL standards committee to add a data type of UUID, I would. But apparently that committee is more secretive than the Soviet Politburo and slower than a glacier.

Bertha answered 3/11, 2013 at 9:45 Comment(1)
> that committee is more secretive than the Soviet Politburo and slower than a glacier. ... Comment of the year !!!Ocker
C
18

I used the following approach to add UUID and other objects to postgres:

 PGobject toInsertUUID = new PGobject();
 toInsertUUID.setType("uuid");
 toInsertUUID.setValue(uuid.toString());
 PreparedStmt stmt = conn.prepareStatement(query);
 stmt.setObject(placeHolder,toInsertUUID);
 stmt.execute();

This way you will be stopping yourself from doing type casting. This piece of code worked perfectly for me for any time for example even json.

Cushman answered 31/7, 2013 at 11:44 Comment(2)
where does PGobject come from ?Gesticulate
@PaulTaylor, org.postgresql.util.PGobject comes from PostgreSQL JDBC driver, org.postgresql:postgresql.Venator
B
14

This worked for me using the org.postgresql.postgresql 42.2.5

myPreparedStatement.setObject(4, UUID.randomUUID(),java.sql.Types.OTHER)

Without java.sql.Types.OTHER I got an error

Brenza answered 16/1, 2019 at 10:6 Comment(1)
Thank you so much, I've had a real nightmare of a time trying to find the right solution to this.Veinlet
D
-1

try

.setParameter("uuid", uuid, PostgresUUIDType.INSTANCE);
Drais answered 15/12, 2016 at 15:7 Comment(2)
Where does PostgresUUIDType.INSTANCE come from, seems to be Hibernate class ?Gesticulate
org.hibernate.typeDrais

© 2022 - 2024 — McMap. All rights reserved.