简体   繁体   English

在不指定列名的情况下在 jOOQ 中插入记录

[英]Inserting record in jOOQ without specifying column names

I have a table USER_ROLES that has 5 columns.我有一个包含 5 列的表 USER_ROLES。 Also, a class UserRole that has the same number of fields and names as USER_ROLES.此外,class UserRole 具有与 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?我是否被迫指定列名?

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:如果您生成了UserRolesRecord ,并且您的UserRole类遵循DefaultRecordMapper定义的命名约定,那么您可以将自定义UserRole内容加载到记录中,如下所示:

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

You can do the following (simpler):您可以执行以下操作(更简单):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM