I recently ran into a strange issue around retrieving data from a JOOQ
record. I wanted to post it here before logging a bug officially in GitHub.
The issue is basically an inconsistency around how you need to access the
result of a select depending on how that query is executed. The following
code illustrates:
String someFieldName = "foo"
Field<?> groupBreakdownField = field("P."+someFieldName); //P.foo
return dsl.select( groupBreakdownField, countDistinct(field("P.id")).as(
"thecount") )
.from( table("person").as("P"))
.groupBy(groupBreakdownField)
.fetch().toStream()
.map( r- > new Group(r.getValue(groupBreakdownField,
String.class), r.getValue("thecount", Integer.class)))
.collect( ....
The query constructed above is goes something like this
SELECT P.foo, count(distinct P.id) as thecount
FROM person P
GROUP BY P.foo
Also note that I'm using groupBreakdownField to map the rows of the result
set to my pojo. In the above example everything works fine.
Now if we change the query to execute a SQL created by JOOQ then something
breaks.
String someFieldName = "foo"
Field<?> groupBreakdownField = field("P."+someFieldName); //P.foo
return dsl.fetch(select( groupBreakdownField, countDistinct(field("P.id")).
as("thecount") )
.from( table("person").as("P"))
.groupBy(groupBreakdownField)
).toStream()
.map( r- > new Group(r.getValue(groupBreakdownField, String.class
), r.getValue("thecount", Integer.class)))
.collect( ....
The above code is similar but instead of calling fetch on the JOOQ query
I'm calling fetch and passing it String SQL created by jooq DSL. The query
run by the DB is exactly the same, however we get the following error when
trying to pull the values out of the result
Caused by: java.lang.IllegalArgumentException: Field (P.foo) is not
contained in Row (foo, thecount)
at org.jooq.impl.Utils.indexOrFail(Utils.java:1059) ~[jooq-3.6.2.jar:na]
at org.jooq.impl.AbstractRecord.getValue(AbstractRecord.java:284)
~[jooq-3.6.2.jar:na]
at org.jooq.impl.AbstractRecord.getValue(AbstractRecord.java:295)
~[jooq-3.6.2.jar:na]
Now JOOQ doesn't understand P.foo. It seems it's not respecting my alias.
If change r.getValue(groupBreakdownField, String.class) to r.getValue("foo",
String.class) then everything works.
Why does it not complain in the first case that it can't find P.foo?
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.