5

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?

ps0604
  • 1,227
  • 23
  • 133
  • 330

2 Answers2

5

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();
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • interesting that you cannot set the column list to a partial list and use `valuesOfRecords` because of same error, nor can you use `set` with more than one record. – Jayson Minard Jun 15 '22 at 19:33
  • @JaysonMinard: The optimal place to direct feature requests is here: https://github.com/jOOQ/jOOQ/issues/new/choose – Lukas Eder Jun 16 '22 at 06:20
1

You can do the following (simpler):

UserRole ur = new UserRole();
create.insertInto(USER_ROLES).set(ur).execute();