Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing columns of manually declared aliased tables in JOOQ

Tags:

java

sql

jooq

I am manually declaring tables and fields for simple (but type safe) queries in JOOQ using:

Table<Record> myTable = DSL.table(DSL.name("my_table"));
Field<String> myField = DSL.field(DSL.name("my_table", "my_field"), String.class);

Creating an alias for myTable is easy:

Table<Record> myAlias = myTable.as("a");

But how can I access the value of myField within myAlias?

Note that myAlias.field(myField) will yield null, as the field is not part of this table definition.

like image 935
Timo Avatar asked Apr 12 '16 14:04

Timo


1 Answers

You cannot access fields from plain SQL tables, as jOOQ doesn't know anything about them. You'll have to construct individual field references for each alias, e.g.

Field<?> myAliasedField = field(name("a", "my_field"), String.class);
like image 170
Lukas Eder Avatar answered Sep 23 '22 18:09

Lukas Eder