Search code examples
jooq

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


I am learning from the manual here: https://www.jooq.org/doc/latest/manual/sql-execution/fetching/pojos/#NA6504

And i'm trying to create a Record from a pojo and i don't have available the record.store() method, or dslContext.executeInsert(newRecord).

My code looks as follows:

// 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 is generated by joooq: open class DummyTable: TableImpl<Record>.

From the documentation, my understanding is that dslContext.newRecord(DUMMY_TABLE, newPojo).store() should just work.

I'm sure I'm missing something obvious here, but I'm not sure where to start looking for.


Solution

  • The problem is that you're not using the most appropriate record type in your variable declaration:

    // 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)
    

    In order to get the table records generated, please activate the relevant flag in your code generation configuration:

    It should be enabled by default.