[英]JOOQ. How to record to DB with insertInto() a multiple fields in one transaction
I need to create one transaction and execute .insertInto() for multiple unspecified fields of certain tables. 我需要创建一个事务并为某些表的多个未指定字段执行.insertInto() 。 I have a problem that transaction is runned successful, but no records is stored.
我有一个问题,即事务运行成功,但是没有存储任何记录。 I think the root clause is not proper execution of .insertInto() method or DSLContext has fail interaction with the wrapper Configuration .
我认为root子句不能正确执行.insertInto()方法,或者DSLContext与包装器Configuration交互失败。 I will be very greatful for any suggestion.
我将非常感谢任何建议。
I have two methods in two classes (tables processing and DAL). 我在两个类中有两个方法(表处理和DAL)。 Fisrt for transaction creation and sending to DAL-class fields to insert.
Fisrt用于创建事务并将其发送到DAL类字段以进行插入。 Second, DAL-class, for inserting a new field.
第二,DAL类,用于插入新字段。
public Boolean insertToMainDB(List<TableForMainDb> mainTables) throws AppDataAccessLayerException {
Boolean InsertTransactSuccessFlag = false;
try {
TransactionalCallable <Boolean> transactional = new TransactionalCallable<Boolean>() {
@Override
public Boolean run(Configuration configuration) throws Exception {
for (TableForMainDb table : mainTables) {
table.getRecorder().recordToDB(table, configuration);
}
return true;
}
};
InsertTransactSuccessFlag = context.transactionResult(transactional);
} catch (DataAccessException ex) {
throw new AppDataAccessLayerException(ex);
}
return InsertTransactSuccessFlag;
}
Second method in DAL class: DAL类中的第二种方法:
public boolean recordToDB(TableForMainDb mainDBtable, Configuration configuration) {
boolean InsertFlag = false;
for (String key : mainDBtable.fields.keySet()){
//using(configuration).
insertInto (
table(mainDBtable.getTableName())
,field(mainDBtable.fields.get(key).getFieldName())
,value(mainDBtable.fields.get(key).getFieldValue())
).attach(configuration);
InsertFlag = true; //TBD
}
return InsertFlag;
}
Transaction runs successful, but records is not inserted to DB. 事务运行成功,但记录未插入数据库。
In Debug mode I see that objects for DSLContext and Configuration are full and contain record to insert date (fields). 在调试模式下,我看到DSLContext和Configuration的对象已满,并且包含用于插入日期(字段)的记录。 The dumps is below.
转储在下面。 The transactional field = false in context.
上下文中的交易字段= false。
Configuration DefaultConfiguration [ connected=true, transactional=true, dialect=POSTGRES, data={org.jooq.configuration....}, settings=... 配置默认配置 [已连接=真,事务性=真,方言= POSTGRES,数据= {org.jooq.configuration ....},设置= ...
DSLContext DefaultConfiguration [ connected=true, transactional=false, dialect=POSTGRES, data={}, settings=... DSLContext DefaultConfiguration [已连接= true,transactional = false,方言= POSTGRES,data = {},设置= ...
您永远不会在Insert
语句上调用Query.execute()
Thank you Lukas for the comment and for the great JOOQ. 感谢Lukas的评论和出色的JOOQ。
we implement insert as 我们实现插入为
.insertInto(Table<Record>, Fields[], Object[] ).execute()
we prepared fields as array of Field[] and values as array of Object[] and than pass it to .insertInto() 我们将字段准备为Field []的数组,将值准备为Object []的数组,然后将其传递给.insertInto()
fieldsAndValuesObject it is object of wrapper class for fields and values (add/get/set). fieldsAndValuesObject是字段和值(添加/获取/设置)包装类的对象。
DSL.using(configuration).insertInto(DSL.table(tableName),
fieldsAndValuesObject.getArrayAllFields())
.values(fieldsAndValuesObject.getArrayAllValues()
)
.execute();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.