jooq `myPojo` 到 `record`: `store` 功能在记录中不可用

jooq `myPojo` to `record`: `store` function is not available on the record

我在这里学习手册:https://www.jooq.org/doc/latest/manual/sql-execution/fetching/pojos/#NA6504

我正在尝试从 pojo 创建记录,但我没有可用的 record.store() 方法,或 dslContext.executeInsert(newRecord).

我的代码如下所示:

// this is created by me
data class Dummy(
    val name: String,
    val id: Long,
    val thisDoesNotExistInTheDatabaseAndThatsFineBecauseMappingToMyBusinessClassStillWorks: String?,
)

// table -> Business Object works ✅ 
dslContext.select()
.from(DummyTable.DUMMY_TABLE)
.fetch()
.into(Dummy::class.java)


// Business Object -> table does not work ❌ 
val newPojo = Dummy("string1", 1, null)
val newRecord: Record = dslContext.newRecord(DUMMY_TABLE, newPojo)
newRecord.store()    // this throws compilation error: Unresolved reference: store
dslContext.executeInsert(newRecord) // this throws compilation error: Type mismatch: inferred type is Record but TableRecord<*>! was expected

DUMMY_TABLE 由 joooq 生成:open class DummyTable: TableImpl<Record>.

根据文档,我的理解是 dslContext.newRecord(DUMMY_TABLE, newPojo).store() 应该可以正常工作。

我确定我在这里遗漏了一些明显的东西,但我不确定从哪里开始寻找。

问题是您没有在变量声明中使用最合适的记录类型:

// This
val newRecord: Record = dslContext.newRecord(DUMMY_TABLE, newPojo)

// ... should be this:
val newRecord: DummyTableRecord = dslContext.newRecord(DUMMY_TABLE, newPojo)

// ... or even this:
val newRecord = dslContext.newRecord(DUMMY_TABLE, newPojo)

为了生成 table 条记录,请在您的代码生成配置中激活相关标志:

它应该默认启用。