Jooq: java.lang.ClassCastException: org.jooq.impl.RecordImpl cannot be cast to org.jooq.generated.{folder}.tables.records....

Created on 15 Sep 2016  路  3Comments  路  Source: jOOQ/jOOQ

when i get UpdateDate 's value from database, i get an error with this description :

java.lang.ClassCastException: org.jooq.impl.RecordImpl cannot be cast to org.jooq.generated.{folder}.tables.records.{TableName}Record

for (int i = 0; i < rows.size(); i++) {
                  rows.get(i).getUpdateDate();.......}

Type of rows is Result<{TableName}Record>

I can change rows type to Result<Record> and get UpdateDate with

for (int i = 0; i < rows.size(); i++) {
                    rows.get(i).get( {TableName}.{TABLENAME}.UPDATE_DATE );......}

but i want to use getUpdateDate()

Functionality Medium Answered Support request

Most helpful comment

When you select(...), you're creating your own ad-hoc record type, which is implemented by the internal RecordImpl. The public type you're getting is Record. You cannot cast that to {Tbl}Record, because you simply don't have an instance of {Tbl}Record. There are two remedies:

// This will fetch all columns from your table
Result<MyTableRecord> rows1 = create.selectFrom(MY_TABLE).fetch();

// This will fetch some columns for your table and explicitly convert Record to MyTableRecord (you cannot cast)
Result<MyTableRecord> rows2 = create.select(MY_TABLE.A, MY_TABLE.B).from(MY_TABLE).fetchInto(MY_TABLE); // or fetch().into(MY_TABLE)

Note that if you do this, you can only select columns from MY_TABLE, no other expressions, functions, etc.

I hope this clarifies things?

All 3 comments

Hmm, it looks like you have done an unsafe cast of a Result<Record> type to a Result<{TableName}Record> type. This is not possible, because your Result really doesn't contain {TableName}Record instances.

How did you produce the rows variable? Can you show the query you were running? Did you use DSLContext.selectFrom()?

I know !!!
rows = DSL.using( this.connection, ... ).create.select( ... ).from( {Table}.{TABLE} )....fetch() ;
{Tbl}Record is instance of Record ?
if it is true, no problem in casting from Record to {Tbl}Record !!!
am i right?

When you select(...), you're creating your own ad-hoc record type, which is implemented by the internal RecordImpl. The public type you're getting is Record. You cannot cast that to {Tbl}Record, because you simply don't have an instance of {Tbl}Record. There are two remedies:

// This will fetch all columns from your table
Result<MyTableRecord> rows1 = create.selectFrom(MY_TABLE).fetch();

// This will fetch some columns for your table and explicitly convert Record to MyTableRecord (you cannot cast)
Result<MyTableRecord> rows2 = create.select(MY_TABLE.A, MY_TABLE.B).from(MY_TABLE).fetchInto(MY_TABLE); // or fetch().into(MY_TABLE)

Note that if you do this, you can only select columns from MY_TABLE, no other expressions, functions, etc.

I hope this clarifies things?

Was this page helpful?
0 / 5 - 0 ratings