[英]jooq- fetching a single value
I had a question.. Why do I have repeat what I have selected in the fetch method. 我有一个问题..为什么我重复了我在fetch方法中选择的内容。
Date minDate = getDSLContext()
.select(CON_CAL_INSTANCE.DATE.min())
.from(CON_CAL_INSTANCE)
.join(CON_CAL_TEMPLATES)
.on(CON_CAL_INSTANCE.TEMPLATE_ID.eq(CON_CAL_TEMPLATES.ID))
.where(CON_CAL_TEMPLATES.ENTRY_TYPE.in("REPT", "HRPT"))
.fetchOne(CON_CAL_INSTANCE.DATE.min());
So I have provided CON_CAL_INSTANCE.DATE.min()
in my select clause, why do I have to repeat it in fetchOne(CON_CAL_INSTANCE.DATE.min())
? 所以我在select子句中提供了
CON_CAL_INSTANCE.DATE.min()
,为什么我必须在fetchOne(CON_CAL_INSTANCE.DATE.min())
重复它?
Or am I not doing this right? 或者我不是这样做的吗?
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. 使用Java泛型构建jOOQ DSL的方式,即使
ResultQuery
使用Record1
作为行类型, ResultQuery<Record1<Date>>
也不会“知道”它只选择单个值。
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 . 另请参见
DSLContext.fetchValue()
Javadoc 。
On a side-note, there had been discussions in the past about using a more LINQ-style syntax in the jOOQ API: 在旁注中,过去曾讨论过在jOOQ API中使用更多LINQ风格的语法:
from Table
where Predicates
select Projection
What may look like a good idea at first is raising new questions: 一开始看起来好主意的是提出新问题:
ORDER BY
, FOR UPDATE
clauses, which should still be placed after SELECT
. ORDER BY
, FOR UPDATE
子句怎么样,应该在SELECT
之后放置。 ( See this post for details ). UNION
, INTERSECT
and EXCEPT
. UNION
, INTERSECT
和EXCEPT
。 I've also written about the difference between lexical and logical order of operations in SQL, here 我还在这里写了关于SQL中词法和逻辑运算顺序之间的区别
These open issues made us stick with the standard SQL syntax. 这些开放性问题使我们坚持使用标准的SQL语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.