Search code examples
javajooq

Inserting record in jOOQ without specifying column names


I have a table USER_ROLES that has 5 columns. Also, a class UserRole that has the same number of fields and names as USER_ROLES.

I'm trying to insert a row without specifying the column names:

    UserRole ur = new UserRole();
    // UserRole fields setting

    create.insertInto(USER_ROLES).values(ur).execute();

But I get this error when I attempt to create the row:

The number of values must match the number of fields

Am I forced to specify the column names?


Solution

  • If you have generated the UserRolesRecord, and if your UserRole class follows the naming conventions defined by DefaultRecordMapper, then you can load your custom UserRole content into the record like this:

    UserRole ur = new UserRole();
    // ...
    UserRoleRecord rec = new UserRoleRecord();
    rec.from(ur);
    create.insertInto(USER_ROLES).set(rec).execute();