Skip to content
Advertisement

Return primary key value generated by default in H2 database upon INSERT of new row, for UUID type column

When using a UUID data type as the primary key of a table, and asking H2 to generate each UUID value by default upon INSERT of a new record, how does one access the value of the newly generated UUID value?

I am using plain JDBC 4.x in a Java app, if that helps with a solution.


I know SCOPE_IDENTITY function returns a long for a key generated on a column marked as IDENTITY for an auto-incrementing sequence number. But I am using UUID rather than an incrementing number as my primary key column types.

Advertisement

Answer

Statement::getGeneratedKeys

As seen in comments and the correct Answer by YCF_L, the solution lies in standard JDBC: Call Statement::getGeneratedKeys. This yields a ResultSet of the key values generated by default in the previous use of that statement. This works with PreparedStatement, and works with auto-generating UUID values as the primary key.

Statement.RETURN_GENERATED_KEYS

The catch is that by default you do not get back generated keys. You must activate this feature by passing an extra argument to your Connection::prepareStatement call. The extra argument is an int, using a constant defined on the Statement interface, Statement.RETURN_GENERATED_KEYS. In modern Java that would have likely have been defined as an Enum, but JDBC dates back to the earliest days of Java, so the argument is a simple int.

Example app

Here is a complete example app, in a single file.

When run.

INFO – Reporting generated keys.

generated keys: 9c6ce984-151b-4e64-8334-d96e17be9525

INFO – Reporting all rows in table person_.

Person: 9c6ce984-151b-4e64-8334-d96e17be9525 | Jesse Johnson

If you want to insert multiple rows at a time, rather than one, use batching. See: Java: Insert multiple rows into MySQL with PreparedStatement.

If you have multiple columns being auto-generated, rather than just the one single UUID column seen here, see the other Answer by YCF_L.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement