如何将 'InsertQuery' 与通用记录和 table 类型一起使用
How can I use 'InsertQuery' with generic record and table types
我有这个方法:
public InsertQuery<Record> getInsertQuery(List<?> pojoList, TableImpl<Record> table) {
InsertQuery<Record> insertQuery = ctx.insertQuery(table);
pojoList.forEach(pojo -> insertQuery.addRecord(ctx.newRecord(table, pojo)));
insertQuery.setReturning();
return insertQuery;
}
它基本上接受一个pojoList
,插入到给定的table
,和returns一个InsertQuery<Record>
,可以执行插入一行或多行.但是当我像这样调用这个方法时:
try (DSLContext ctx = DslContextFactory.getDslContext();
InsertQuery<AttachmentsRecord> insertQuery = new AttachmentQueries(ctx)
.getInsertQuery(pojos, JOOQ_GENERATED_TABLE)) {
// do something.
}
它在 JOOQ_GENERATED_TABLE
上抱怨说所需的类型是 TableImpl<Record>
,但提供了其他内容。如何为 jooq 生成的表和记录提供通用类型?真的需要一些帮助。
将您的实用程序更改为:
public <R extends Record> InsertQuery<R> getInsertQuery(
List<?> pojoList,
Table<R> table // I recommend using Table<R> because TableImpl<R> is internal API
) {
InsertQuery<R> insertQuery = ctx.insertQuery(table);
pojoList.forEach(pojo -> insertQuery.addRecord(ctx.newRecord(table, pojo)));
insertQuery.setReturning();
return insertQuery;
}
我有这个方法:
public InsertQuery<Record> getInsertQuery(List<?> pojoList, TableImpl<Record> table) {
InsertQuery<Record> insertQuery = ctx.insertQuery(table);
pojoList.forEach(pojo -> insertQuery.addRecord(ctx.newRecord(table, pojo)));
insertQuery.setReturning();
return insertQuery;
}
它基本上接受一个pojoList
,插入到给定的table
,和returns一个InsertQuery<Record>
,可以执行插入一行或多行.但是当我像这样调用这个方法时:
try (DSLContext ctx = DslContextFactory.getDslContext();
InsertQuery<AttachmentsRecord> insertQuery = new AttachmentQueries(ctx)
.getInsertQuery(pojos, JOOQ_GENERATED_TABLE)) {
// do something.
}
它在 JOOQ_GENERATED_TABLE
上抱怨说所需的类型是 TableImpl<Record>
,但提供了其他内容。如何为 jooq 生成的表和记录提供通用类型?真的需要一些帮助。
将您的实用程序更改为:
public <R extends Record> InsertQuery<R> getInsertQuery(
List<?> pojoList,
Table<R> table // I recommend using Table<R> because TableImpl<R> is internal API
) {
InsertQuery<R> insertQuery = ctx.insertQuery(table);
pojoList.forEach(pojo -> insertQuery.addRecord(ctx.newRecord(table, pojo)));
insertQuery.setReturning();
return insertQuery;
}