jooq- fetching a single value
You're doing it right. The way the jOOQ DSL is constructed with Java generics, your ResultQuery<Record1<Date>>
doesn't "know" it is selecting only a single value, even if the ResultQuery
uses Record1
as a row type.
Apart from repeating the column, you have some other options:
ResultQuery<Record1<Date>> query = // ...// Use two method calls (this may result in a NullPointerException!// as fetchOne() may return null):Date date1 = query.fetchOne().value1();// Use fetchValue():Date date2 = getDSLContext().fetchValue(query);
See also the DSLContext.fetchValue()
Javadoc.
Using a more LINQ-style syntax
On a side-note, there had been discussions in the past about using a more LINQ-style syntax in the jOOQ API:
from Tablewhere Predicatesselect Projection
What may look like a good idea at first is raising new questions:
- What about the
ORDER BY
,FOR UPDATE
clauses, which should still be placed afterSELECT
. (See this post for details). - What about set operations, like
UNION
,INTERSECT
andEXCEPT
.
I've also written about the difference between lexical and logical order of operations in SQL, here
These open issues made us stick with the standard SQL syntax.