Creating lookup maps with multiple fields as key

42 views
Skip to first unread message

Tariq Bugrara

unread,
May 19, 2015, 9:51:55 AM5/19/15
to jooq...@googlegroups.com
Hey I have a query like this:

Map<List<?>, Boolean> dateRangeExistsMap = context
.select(BLOCK_ASSIGNMENT.START_DATE, BLOCK_ASSIGNMENT.END_DATE, DSL.field(DSL.count().ge(1)))
.from(BLOCK_ASSIGNMENT)
.groupBy(new Field<?>[]{BLOCK_ASSIGNMENT.START_DATE, BLOCK_ASSIGNMENT.END_DATE})
.fetchMap(new Field<?>[]{BLOCK_ASSIGNMENT.START_DATE, BLOCK_ASSIGNMENT.END_DATE},
new RecordMapper<Record3<LocalDate, LocalDate, Boolean>, Boolean>() {
@Override
public Boolean map(Record3<LocalDate, LocalDate, Boolean> record) {
return record.value3();
}
});

I was wondering if there was a better way to create the map so doing a lookup wouldn't require the creation of a list? The only issue I have with this approach now is that the order the elements are inserted must match the order of fields in the fetchMap. 

Lukas Eder

unread,
May 19, 2015, 11:56:36 AM5/19/15
to jooq...@googlegroups.com
Hello Tariq,

Unfortunately, the fetchMap() API was added a bit early in jOOQ's history, which is why it returns a Map<List<?>, Boolean> type rather than a Map<Record, Boolean> type as it should, where Record would probably be a bit more like what you're looking for.

From your example, I take that you're not using Java 8 yet, where custom grouping via Streams and lambdas becomes easier to implement at your side.

A workaround would be to use only START_DATE for grouping, as I would imagine that your blocks are mutually exclusive date ranges... Otherwise, you'll probably have to write (or use) some third-party library code yourself, I'm afraid.

Cheers,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tariq Bugrara

unread,
May 19, 2015, 12:11:55 PM5/19/15
to jooq...@googlegroups.com
Unfortunately there are date ranges with the same start date and unfortunately we can't move to Java 8 because we're still using db4o :( but we're moving to MySQL and jOOQ's been great so far.

Thanks for the clarification though! Do you think moving towards using a record instead of a list is something worth being contributed?

Lukas Eder

unread,
May 19, 2015, 12:21:15 PM5/19/15
to jooq...@googlegroups.com
2015-05-19 18:11 GMT+02:00 Tariq Bugrara <tbug...@gmail.com>:
Unfortunately there are date ranges with the same start date and unfortunately we can't move to Java 8 because we're still using db4o :( but we're moving to MySQL and jOOQ's been great so far.

Interesting, how does db4o prevent using Java 8? Or is this related to your moving to MySQL from another database?

Thanks for the clarification though! Do you think moving towards using a record instead of a list is something worth being contributed?

The relevant issue is this one here:

Thank you very much for your offer. Right now, I don't think we should break compatibility with this change. In fact, let's wait and see if the change is necessary at all. Right now, we're evaluating whether the jOOQ 3.7 should support Java 8 (supporting Java 6 from the commercial jOOQ editions). A lot of great features can be derived if jOOQ Cursors can be turned into Streams directly. Once we support Java 8, we'll also move a bit of jOOλ (https://github.com/jOOQ/jOOL) functionality into jOOQ. In that case, grouping Results will be much easier with jOOQ...

Tariq Bugrara

unread,
May 19, 2015, 12:56:38 PM5/19/15
to jooq...@googlegroups.com
Interesting, how does db4o prevent using Java 8? Or is this related to your moving to MySQL from another database?
 
I actually don't know, as a colleague did the research. We'll be off of it soon anyway, so using Java 8 for this problem will be sufficient. 

Thank you very much for your offer. Right now, I don't think we should break compatibility with this change. In fact, let's wait and see if the change is necessary at all. Right now, we're evaluating whether the jOOQ 3.7 should support Java 8 (supporting Java 6 from the commercial jOOQ editions). A lot of great features can be derived if jOOQ Cursors can be turned into Streams directly. Once we support Java 8, we'll also move a bit of jOOλ (https://github.com/jOOQ/jOOL) functionality into jOOQ. In that case, grouping Results will be much easier with jOOQ...

That sounds very exciting, I'll be looking out for it! 

Thanks again,
Tariq

Tariq Bugrara

unread,
May 22, 2015, 2:26:40 PM5/22/15
to jooq...@googlegroups.com
Hey Lukas,

I'm currently using the fetchGroups(Field[], RecordMapper<Record, T>) method, which does return a Map<Record, T>. Is DSLContext.newRecord the best way to create a record for a lookup?

Thanks!
Tariq

Lukas Eder

unread,
May 26, 2015, 9:10:56 AM5/26/15
to jooq...@googlegroups.com
2015-05-22 20:26 GMT+02:00 Tariq Bugrara <tbug...@gmail.com>:
Hey Lukas,

I'm currently using the fetchGroups(Field[], RecordMapper<Record, T>) method, which does return a Map<Record, T>.

OK, I wasn't sure if that would be an option for you, as it actually returns a list as a value in Map<Record, List<T>>, compared to Map<?, T> for fetchMap()...
 
Is DSLContext.newRecord the best way to create a record for a lookup?

Yes it is.

Best Regards,
Lukas
Reply all
Reply to author
Forward
0 new messages