I'm using jooq, and I'm liking the fetchMap(Field) method. It works great if I want to use a single field as the key. Now, I'd like to fetch a map using two fields as the key. The fetchMap(Field[]) method returns a map with Record as the key. I have the values of the fields in the key, but I'm not sure how to convert them to a Record object so that I can grab the value from the map.

Basically, I'm looking for a way to instantiate a Record from a set of field values.

More info: I've got a table of provincial reports, for each week of the year. The table key is (province_id, week_number). I can do this...

DSLContext create = DSL.using(connection, SQLDialect.ORACLE);
Map<Record, ReportRecord> results = create.selectFrom(Tables.Report).fetchMap(new Field[]{Tables.Report.province_id, Tables.Report.week_number});

But now, let's say I have a province_id of 2, and a week_number of 42. I want to create a Record from that, so I can do

ReportRecord report = results.get(record);

Right now, I've switched to calling

Result<ReportRecord> results = create.selectFrom(Tables.Report).fetch();

, iterating over it, and creating a map myself. It's working, but I'd really like to know how the fetchMap method is meant to be used. Couldn't find anything in the jooq manual or the javadoc.

有帮助吗?

解决方案

Hmm, there actually isn't a very easy way to do this. What you can do is:

Record record = create.newRecord(Report.province_id, Report.week_number);
record.setValue(Report.province_id, 2);
record.setValue(Report.week_number, 42);
ReportRecord report = results.get(record);

Agreed, that's a bit verbose and not very user friendly. This should be improved in jOOQ 3.3 with #2847

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top