jooq-获取单个值 [英] jooq- fetching a single value

查看:55
本文介绍了jooq-获取单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.为什么我要重复在提取方法中选择的内容.

I had a question.. Why do I have repeat what I have selected in the fetch method.

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());

所以我在select子句中提供了CON_CAL_INSTANCE.DATE.min(),为什么我必须在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())?

还是我做错了吗?

推荐答案

您做对了.使用Java泛型构造jOOQ DSL的方式,您的 ResultQuery<Record1<Date>> 不会知道" ,它只是选择一个值,即使ResultQuery使用

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);

另请参见 DSLContext.fetchValue() Javadoc .

See also the DSLContext.fetchValue() Javadoc.

附带说明,过去曾经讨论过在jOOQ API中使用更多LINQ风格的语法:

On a side-note, there had been discussions in the past about using a more LINQ-style syntax in the jOOQ API:

from Table
where Predicates
select Projection

一开始看起来不错的主意是提出新的问题:

What may look like a good idea at first is raising new questions:

  1. ORDER BYFOR UPDATE子句如何处理,这些子句仍应放在SELECT之后. (有关详细信息,请参阅此帖子).
  2. 关于设置操作,例如UNIONINTERSECTEXCEPT.
  1. What about the ORDER BY, FOR UPDATE clauses, which should still be placed after SELECT. (See this post for details).
  2. What about set operations, like UNION, INTERSECT and EXCEPT.

我也在这里写过关于SQL的词法和逻辑顺序之间的区别的文章,

这些未解决的问题使我们坚持使用标准SQL语法.

These open issues made us stick with the standard SQL syntax.

这篇关于jooq-获取单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆