1

I'm using jOOQ (3.14.11) to manage a table defined (in H2 or MYSQL) as:

CREATE TABLE example_one (
    group_id VARCHAR(36) NOT NULL, 
    pawn_id INT UNSIGNED AUTO_INCREMENT NOT NULL, 

    some_unimportant_attribute INT UNSIGNED DEFAULT 0 NOT NULL, 
    another_uniportant_attribute VARCHAR(36), 

    CONSTRAINT pk_example_one PRIMARY KEY (group_id, pawn_id)
)

Note that in this SQL, the primary key specifies the (group, pawn) IDs in that order but it is the pawn_id, the second one, which is the auto-increment/identity column.

It appears that jOOQ doesn't like this arrangement. When I try to use the Record objects to insert a new row, it will not return back to me the "pawnID" value:

 ExampleOneRecord r = create.newRecord(EXAMPLE_ONE);
 r.setGroup("a group identity");
 r.store();

 assert r.getPawnId() != null;  // <---- FAILS test

Diving into the code, the suspect seems to be in AbstractDMLQuery.java method executeReturningGeneratedKeysFetchAdditionalRows which has this bit of logic:

            // Some JDBC drivers seem to illegally return null
            // from getGeneratedKeys() sometimes
            if (rs != null)
                while (rs.next())
                    list.add(rs.getObject(1));

The call to rs.getObject(1) seems to be assuming that the generated column will always be the first column of the primary key.

Is there any way to convince jOOQ otherwise?

goolie
  • 145
  • 1
  • 7
  • There's probably a bug in there, on account that an identity / auto_increment column is by itself a great candidate key, and should be the primary key in your case. What's the rationale of putting `group_id` in that key together with the auto increment column? Making it part of the clustering index? This is the first time I've seen this practice... – Lukas Eder Jun 07 '21 at 07:16
  • Yes I figured this was probably a fairly unusual case. The rationale was partly due to query efficiency (we sometimes might have multiple "group" levels) so we can query for and manage the "children". Also because we have multiple DBs and might need to move 'groups' between them -- which would make the "pawn_id" not sufficiently unique (and using UUIDs for them kills the clustering and bloats the storage). – goolie Jun 07 '21 at 12:09
  • 1
    Very interesting, thanks for sharing. I'll investigate whether this is a bug in jOOQ that can be fixed, or whether we'll run into a JDBC driver limitation here. Will revert back here with findings – Lukas Eder Jun 07 '21 at 13:22
  • What MySQL version are you using? On 8.0.24, I'm getting this error when I try your `CREATE TABLE`: *"SQL Error [1075] [42000]: Incorrect table definition; there can be only one auto column and it must be defined as a key"*. Do you have any additional unique constraints? – Lukas Eder Jun 08 '21 at 07:37
  • I can't reproduce this with MySQL version 8.0.24, driver version `mysql:mysql-connector-java:8.0.25` and said unique constraint. I think an MCVE would be helpful here: https://github.com/jOOQ/jOOQ-mcve, but it's likely this is just a bug in an older server or driver version. – Lukas Eder Jun 08 '21 at 07:56
  • Note, I've filed a bug to MySQL. There seems to be something weird going on with those table definitions: https://bugs.mysql.com/bug.php?id=103939. – Lukas Eder Jun 08 '21 at 08:05
  • Sorry I edited the `CREATE TABLE` statement to include the `INDEX` declaration which seems to be required by MySQL for this kind of arrangement (I'm using 8.0.13). And thanks for filing the MySQL bug! – goolie Jun 08 '21 at 12:05
  • Can you try on your side using 8.0.25? – Lukas Eder Jun 08 '21 at 12:58
  • Well it looks like MySQL, once you get the table created, works with jOOQ just fine in this case. It's only H2 that I have the problem with. H2 returns all columns of the primary key in the jdbc `getGeneratedKeys`, even the non-generated ones, which is tripping up jOOQ. Whatever MySQL returns (I didn't look) seems to be working fine. – goolie Jun 08 '21 at 22:58
  • I tried with H2 1.4.200 now, can't reproduce it either. I guess an MCVE would really help here: https://github.com/jOOQ/jOOQ-mcve – Lukas Eder Jun 24 '21 at 11:15
  • MCVE at: https://github.com/sgilhooly/jOOQ-mcve – goolie Jun 25 '21 at 21:22
  • Thanks for your patience. I was able to reproduce the issue with your MCVE. Will look into fixing this, soon. – Lukas Eder Jul 14 '21 at 15:11

1 Answers1

0

This is a bug in jOOQ 3.15.1 for H2: https://github.com/jOOQ/jOOQ/issues/12192

It has been fixed in jOOQ 3.16.0 for H2 2.0.202, which now supports the powerful data change delta table syntax, allowing for much easier fetching of generated values from a DML statement (it was implemented before that H2 version, but had a significant bug: https://github.com/h2database/h2database/issues/2502)

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509