在 jooq 中使用 row() 将结果映射到自定义 seekStep class
Using row() in jooq to map result into custom seekStep class
因为我不想使用 RecordX class(在我的例子中,我从查询中返回 11 个字段)我创建了自定义映射器以将 SelectSeekStep 结果映射到其中。当我的字段属于 joined table 的一部分时,就会出现问题。例如
@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;
}
假设除 test_id
字段之外的所有字段都是主 table 的一部分(我们称之为 dummy
)并且该字段是 test
[=38] 的一部分=] 我们将连接到 left join
.
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);
我得到异常:
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"
))
更新:添加了整个方法调用:
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);
}
内部进程记录我正在将 Record
个对象映射到所需的对象类型。
void processRecords(List<Records> records) {
record.map(recordMapper);
}
我有自定义的 recordMapper 实现,我正在做这样的逻辑:
testName = record.get(TEST.TEST_NAME, Test.class);
堆栈跟踪:
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[=17=](Repository.java:78)
at org.jooq.impl.DefaultDSLContext.lambda$transaction(DefaultDSLContext.java:611)
at org.jooq.impl.DefaultDSLContext.lambda$transactionResult0(DefaultDSLContext.java:549)
问题是您将记录嵌套为这样的类型
Cursor<Record1<CustomMapperSelectSeekStep>>
当你认为自己拥有这样的类型时:
Cursor<Record10<T1, T2, ..., T10>>
在后一种情况下,您可以像以前一样简单地提取投影列:
record.get(TEST.TEST_NAME);
但是当您有嵌套记录时,该列不再存在,它嵌套在匿名 Record1<?>
类型中。不仅如此,因为您使用了 ad-hoc converter using convertFrom()
,不再有嵌套记录,但是 jOOQ 为您投影了您的自定义 POJO 类型 CustomMapperSelectSeekStep
。
因此,只需像这样读取您的值:
void processRecords(List<Record1<CustomMapperSelectSeekStep>> records) {
for (Record1<CustomMapperSelectSeekStep> record : records) {
CustomMapperSelectSeekStep pojo : record.value1();
}
}
因为我不想使用 RecordX class(在我的例子中,我从查询中返回 11 个字段)我创建了自定义映射器以将 SelectSeekStep 结果映射到其中。当我的字段属于 joined table 的一部分时,就会出现问题。例如
@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;
}
假设除 test_id
字段之外的所有字段都是主 table 的一部分(我们称之为 dummy
)并且该字段是 test
[=38] 的一部分=] 我们将连接到 left join
.
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);
我得到异常:
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"
))
更新:添加了整个方法调用:
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);
}
内部进程记录我正在将 Record
个对象映射到所需的对象类型。
void processRecords(List<Records> records) {
record.map(recordMapper);
}
我有自定义的 recordMapper 实现,我正在做这样的逻辑:
testName = record.get(TEST.TEST_NAME, Test.class);
堆栈跟踪:
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[=17=](Repository.java:78)
at org.jooq.impl.DefaultDSLContext.lambda$transaction(DefaultDSLContext.java:611)
at org.jooq.impl.DefaultDSLContext.lambda$transactionResult0(DefaultDSLContext.java:549)
问题是您将记录嵌套为这样的类型
Cursor<Record1<CustomMapperSelectSeekStep>>
当你认为自己拥有这样的类型时:
Cursor<Record10<T1, T2, ..., T10>>
在后一种情况下,您可以像以前一样简单地提取投影列:
record.get(TEST.TEST_NAME);
但是当您有嵌套记录时,该列不再存在,它嵌套在匿名 Record1<?>
类型中。不仅如此,因为您使用了 ad-hoc converter using convertFrom()
,不再有嵌套记录,但是 jOOQ 为您投影了您的自定义 POJO 类型 CustomMapperSelectSeekStep
。
因此,只需像这样读取您的值:
void processRecords(List<Record1<CustomMapperSelectSeekStep>> records) {
for (Record1<CustomMapperSelectSeekStep> record : records) {
CustomMapperSelectSeekStep pojo : record.value1();
}
}