[英]JOOQ fetchLazy into POJO
So I'd like to fetch some records lazyly into POJOs using DSLContext
like so:因此,我想使用
DSLContext
将一些记录懒惰地提取到 POJO 中, DSLContext
所示:
public Iterator<Something> getSomething(DSLContext dsl) {
return dsl.selectDistinct(STUFF.FIELD)
.from(STUFF)
.fetchLazyInto(Something.class)
.iterator();
}
The problem is that there's no such fetchLazyInto()
and I cant do something like fetchLazy().into(Something.class)
neither问题是没有这样的
fetchLazyInto()
并且我不能做类似fetchLazy().into(Something.class)
So how do I go about lazy fetching into a POJO?那么我该如何懒惰地获取 POJO 呢? I'd rather avoid coding a
RecordMapper
我宁愿避免编码
RecordMapper
I certainly could do the following but it feels wrong:我当然可以执行以下操作,但感觉不对:
dsl.selectDistinct(STUFF.FIELD)
.from(STUFF)
.fetchLazy()
.stream()
.map(Something::new) //now Something constructor is ugly
.iterator();
Thanks!谢谢!
This could be done using jOOQ API only as follows:这只能使用 jOOQ API 完成,如下所示:
dsl.selectDistinct(STUFF.FIELD)
.from(STUFF)
.fetchLazy()
.stream()
.map(r -> r.into(Something.class))
.iterator();
However, do note that Iterator
does not extend AutoCloseable
and as such, you might have a dangling resource (the Cursor
's underlying JDBC ResultSet
), which isn't closed as early as you stop processing the results.但是,请注意
Iterator
不扩展AutoCloseable
,因此,您可能有一个悬空资源( Cursor
的底层 JDBC ResultSet
),它不会在您停止处理结果时尽早关闭。 It will be closed only when you complete processing without errors, or when the ResultSet
is garbage collected.只有当您完成处理而没有错误时,或者当
ResultSet
被垃圾收集时,它才会关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.