[英]Using row() in jooq to map result into custom seekStep class
Since I don't want to use RecordX class (in my case I am returning 11 fields from query) I've created custom mapper to map SelectSeekStep result into it.由于我不想使用 RecordX class(在我的例子中,我从查询中返回 11 个字段)我创建了自定义映射器到 map SelectSeekStep 结果。 The problem occurs when I have fields that are part of joined table.
当我的字段属于连接表的一部分时,就会出现问题。 Eg
例如
@Value
public class CustomMapperSelectSeekStep {
private final String field1;
private final String field2;
private final String field3;
private final String field4;
private final String field5;
private final String field6;
private final String field7;
private final String test_id;
private final String field9;
private final String field10;
}
Let's say that all fields except test_id
fields are part of main table (let's call it dummy
) and that field is part of test
table which we will connect with left join
.假设除
test_id
字段之外的所有字段都是主表的一部分(我们称它为dummy
)并且该字段是我们将与left join
连接的test
表的一部分。
dslContext
.select(
row(DUMMY.FIELD1,
DUMMY.FIELD2,
DUMMY.FIELD3,
DUMMY.FIELD4,
DUMMY.FIELD5,
DUMMY.FIELD6,
DUMMY.FIELD7,
TEST.TEST_NAME,
DUMMY.FIELD8,
DUMMY.FIELD9,
DUMMY.FIELD10).mapping(CustomMapperSelectSeekStep::new))
.from(DUMMY)
.leftJoin(TEST).on(TEST.ID.eq(DUMMY.TEST_ID))
.orderBy(DUMMY.FIELD1);
Exception I am getting:我得到的异常:
java.lang.IllegalArgumentException: Field ("test"."test_name") is not contained in Row
(row (
"DUMMY"."FIELD1",
"DUMMY"."FIELD2",
"DUMMY"."FIELD3",
"DUMMY"."FIELD4",
"DUMMY"."FIELD5",
"DUMMY"."FIELD6",
"DUMMY"."FIELD7",
"DUMMY"."FIELD8",
"TEST"."TEST_NAME",
"DUMMY"."FIELD9",
"DUMMY"."FIELD10"
))
UPDATE: added entire method call:更新:添加了整个方法调用:
dslContext.transaction(configuration -> {
DSLContext localDsl = DSL.using(configuration);
try (Cursor<Record1<CustomMapperSelectSeekStep>> records =
selectRecords(localDsl)
.fetchSize(1000)
.resultSetType(ResultSet.TYPE_FORWARD_ONLY)
.resultSetConcurrency(ResultSet.CONCUR_READ_ONLY)
.fetchLazy()) {
processRecords(records);
}
});
//method
private SelectSeekStep1<Record1<CustomMapperSelectSeekStep>,Timestamp> selectRecords(DSLContext dslContext) {
return dslContext.select(
row(DUMMY.FIELD1,
DUMMY.FIELD2,
DUMMY.FIELD3,
DUMMY.FIELD4,
DUMMY.FIELD5,
DUMMY.FIELD6,
DUMMY.FIELD7,
TEST.TEST_NAME,
DUMMY.FIELD8,
DUMMY.FIELD9,
DUMMY.FIELD10).mapping(CustomMapperSelectSeekStep::new))
.from(DUMMY)
.leftJoin(TEST).on(TEST.ID.eq(DUMMY.TEST_ID))
.orderBy(DUMMY.FIELD1);
}
Inside process record I am mapping Record
objects to desired object type.在进程记录内部,我将
Record
对象映射到所需的 object 类型。
void processRecords(List<Records> records) {
record.map(recordMapper);
}
I have custom recordMapper implementation where i am doing logic like this:我有自定义的 recordMapper 实现,我正在做这样的逻辑:
testName = record.get(TEST.TEST_NAME, Test.class);
Stack trace:堆栈跟踪:
at org.jooq.impl.Tools.indexFail(Tools.java:1769)
at org.jooq.impl.AbstractRecord.get(AbstractRecord.java:331)
at org.jooq.impl.AbstractRecord.get(AbstractRecord.java:336)
at org.test.CustomMapper.map(CustomMapper.java:42)
at org.test.CustomMapper.map(CustomMapper.java:16)
at org.jooq.impl.AbstractRecord.map(AbstractRecord.java:904)
at org.test.Processor.processRecords(Processor.java:88)
at org.test.Repository.lambda$fetchLazy$0(Repository.java:78)
at org.jooq.impl.DefaultDSLContext.lambda$transaction$5(DefaultDSLContext.java:611)
at org.jooq.impl.DefaultDSLContext.lambda$transactionResult0$3(DefaultDSLContext.java:549)
The problem is that you've nested your record to have a type like this问题是您将记录嵌套为具有这样的类型
Cursor<Record1<CustomMapperSelectSeekStep>>
When you thought you had a type like this:当你认为你有这样的类型时:
Cursor<Record10<T1, T2, ..., T10>>
In the latter case, you could simply extract the projected column like you did:在后一种情况下,您可以像以前一样简单地提取投影列:
record.get(TEST.TEST_NAME);
But when you have a nested record, that column no longer exists, it's nested in an anonymous Record1<?>
type.但是当您有嵌套记录时,该列不再存在,它嵌套在匿名
Record1<?>
类型中。 Not only that, but because you used an ad-hoc converter using convertFrom()
, there's no nested record anymore, but jOOQ projected your custom POJO type CustomMapperSelectSeekStep
for you.不仅如此,因为您使用了一个使用
convertFrom()
的临时转换器,所以不再有嵌套记录,但是 jOOQ 为您投影了您的自定义 POJO 类型CustomMapperSelectSeekStep
。
So, just read your value like this:因此,只需像这样阅读您的价值:
void processRecords(List<Record1<CustomMapperSelectSeekStep>> records) {
for (Record1<CustomMapperSelectSeekStep> record : records) {
CustomMapperSelectSeekStep pojo : record.value1();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.