简体   繁体   中英

JOOQ fetchLazy into POJO

So I'd like to fetch some records lazyly into POJOs using DSLContext like so:

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

So how do I go about lazy fetching into a POJO? I'd rather avoid coding a 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:

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. It will be closed only when you complete processing without errors, or when the ResultSet is garbage collected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM