Jooq - 使用 UpdatableRecord 从 select 订购结果

Jooq - Order results from select with UpdatableRecord

如何使用 UpdatableRecord 对查询结果进行排序来执行 select?

查看手册,例如

create.fetch(BOOK, BOOK.PUBLISHED_IN.equal(1948))

如何添加订单条款?

我正在使用 Jooq 3.6.4

感谢您的帮助

更新:

我知道我可以使用,例如:

    for(UtenteRecord utenteRecord : 
        create.selectFrom(UTENTE).orderBy(UTENTE.LOGIN)){
    }

但它似乎在结尾有和没有 fetch() 的情况下都有效,为什么?

正在数据库中排序

只需写出 SQL 查询并使用 selectFrom() 到 return UpdatableRecord:

DSL.using(configuration)
   .selectFrom(UTENTE)
   .orderBy(UTENTE.LOGIN)
   .fetch();

旁注:为什么 foreach 循环中不需要 fetch()

您已经注意到您不需要在 foreach 循环中编写 fetch()

for (UtenteRecord record : DSL.using(configuration)
                              .selectFrom(UTENTE)
                              .orderBy(UTENTE.LOGIN)) {
    ...
}

这是因为ResultQuery extends Iterable and when the foreach loop calls Iterable.iterator(), jOOQ will execute the query and return the Result.iterator

I've just written a blog post to illustrate this more in detail

使用客户端排序

请注意,您始终可以在客户端中对结果进行排序,尽管这可能不被推荐,因为您应该将尽可能多的操作推送到数据库中以帮助优化器:

DSL.using(configuration)
   .fetch(UTENTE, somePredicate)
   .sortAsc(UTENTE.LOGIN);