hi,
I am facing below issue. MySQL is datetime(6) and the timezone is UTC and in java we are using Instant.
We have implemented a custom converter:
public class CMSDateTimeConverter implements Converter<LocalDateTime, Instant> {
private static final long serialVersionUID = 1L;
@Override
public Instant from(LocalDateTime t) {
return t == null ? null : t.toInstant(ZoneOffset.UTC);
}
@Override
public LocalDateTime to(Instant u) {
return u == null ? null : LocalDateTime.ofInstant(u, ZoneId.ofOffset("UTC", ZoneOffset.ofHours(0)));
}
@Override
public Class<LocalDateTime> fromType() {
return LocalDateTime.class;
}
@Override
public Class<Instant> toType() {
return Instant.class;
}
}
This custom converter we are using in the jooq xml as below:
<forcedType>
<userType>java.time.Instant</userType>
<converter>org.jooq.util.CMSDateTimeConverter</converter>
<types>DATETIME.*</types>
</forcedType>
Now the Table gets autogenerated by this as below:
public final TableField<AccruedCostAllocationReversalRecord, Integer> VERSION;
public final TableField<AccruedCostAllocationReversalRecord, Instant> CREATED;
public final TableField<AccruedCostAllocationReversalRecord, Instant> LAST_MODIFIED;
private AccruedCostAllocationReversalTable(Name alias, Table<AccruedCostAllocationReversalRecord> aliased, Field<?>[] parameters, Condition where) {
super(alias, (Schema)null, aliased, parameters, DSL.comment(""), TableOptions.table(), where);
this.ID = createField(DSL.name("id"), SQLDataType.INTEGER.nullable(false).identity(true), this, "");
this.ACCRUED_COST_ALLOCATION_ID = createField(DSL.name("accrued_cost_allocation_id"), SQLDataType.INTEGER.nullable(false), this, "");
this.EFFECTIVE_DATE = createField(DSL.name("effective_date"), SQLDataType.LOCALDATETIME(6).nullable(false), this, "", new CMSDateTimeConverter());
this.INVOICE_CURRENCY = createField(DSL.name("invoice_currency"), SQLDataType.CHAR(3).nullable(false), this, "");
this.INVOICE_AMOUNT = createField(DSL.name("invoice_amount"), SQLDataType.DECIMAL(19, 4).nullable(false), this, "");
this.INVOICE_PAYMENT_FX_RATE = createField(DSL.name("invoice_payment_fx_rate"), SQLDataType.DECIMAL(20, 10).nullable(false), this, "");
this.INVOICE_PAYMENT_FX_RATE_PROVIDER = createField(DSL.name("invoice_payment_fx_rate_provider"), SQLDataType.VARCHAR(255).nullable(false), this, "");
this.PAYMENT_CURRENCY = createField(DSL.name("payment_currency"), SQLDataType.CHAR(3).nullable(false), this, "");
this.PAYMENT_AMOUNT = createField(DSL.name("payment_amount"), SQLDataType.DECIMAL(19, 4).nullable(false), this, "");
this.PAYMENT_REPORTING_FX_RATE = createField(DSL.name("payment_reporting_fx_rate"), SQLDataType.DECIMAL(20, 10).nullable(false), this, "");
this.PAYMENT_REPORTING_FX_RATE_PROVIDER = createField(DSL.name("payment_reporting_fx_rate_provider"), SQLDataType.VARCHAR(255).nullable(false), this, "");
this.REPORTING_CURRENCY = createField(DSL.name("reporting_currency"), SQLDataType.CHAR(3).nullable(false), this, "");
this.REPORTING_AMOUNT = createField(DSL.name("reporting_amount"), SQLDataType.DECIMAL(19, 4).nullable(false), this, "");
this.REVERSAL_ERP_GL_JOURNAL_ENTRY_ID = createField(DSL.name("reversal_erp_gl_journal_entry_id"), SQLDataType.INTEGER, this, "");
this.VERSION = createField(DSL.name("version"), SQLDataType.INTEGER.nullable(false), this, "");
this.CREATED = createField(DSL.name("created"), SQLDataType.LOCALDATETIME(6).nullable(false), this, "", new CMSDateTimeConverter());
this.LAST_MODIFIED = createField(DSL.name("last_modified"), SQLDataType.LOCALDATETIME(6).nullable(false), this, "", new CMSDateTimeConverter());
}
Now i am defining the created and lastmodified as instant as below and supplying them in sql context and then inserting it in table i am getting error:
Instant now = LocalDateTime.ofInstant(Instant.now().truncatedTo(ChronoUnit.SECONDS),
ZoneId.ofOffset("UTC", ZoneOffset.ofHours(0))).toInstant(ZoneOffset.UTC);
SelectConditionStep<Record1<Integer>> reversableCostsQuery = buildReversalQuery(context, invoicePayment);
String tempTableName = createTempTableWithIdField(context, invoicePayment, reversableCostsQuery);
try {
SelectHavingConditionStep<Record15<Integer, String, BigDecimal, String,
String, BigDecimal, String, String,
Instant, Integer, Instant, Instant,
BigDecimal, BigDecimal, BigDecimal>> reversalSelect =
context.select(aca.ID, aca.INVOICE_CURRENCY, aca.INVOICE_PAYMENT_FX_RATE, aca.INVOICE_PAYMENT_FX_RATE_PROVIDER,
aca.PAYMENT_CURRENCY, aca.PAYMENT_REPORTING_FX_RATE, aca.PAYMENT_REPORTING_FX_RATE_PROVIDER, aca.REPORTING_CURRENCY,
DSL.val(usedEffectiveDate), DSL.val(1), DSL.val(now), DSL.val(now),
invoiceAmountReversalField, paymentAmountReversalField, reportingAmountReversalField)
.from(joinTempTable(context, aca, aca.ACCRUED_COST_ID, tempTableName))
.leftOuterJoin(acar).on(acar.ACCRUED_COST_ALLOCATION_ID.eq(aca.ID))
.groupBy(aca.ID, aca.INVOICE_CURRENCY, aca.INVOICE_PAYMENT_FX_RATE, aca.INVOICE_PAYMENT_FX_RATE_PROVIDER,
aca.PAYMENT_CURRENCY, aca.PAYMENT_REPORTING_FX_RATE, aca.PAYMENT_REPORTING_FX_RATE_PROVIDER, aca.REPORTING_CURRENCY,
DSL.val(usedEffectiveDate), DSL.val(1), DSL.val(now), DSL.val(now))
.having(invoiceAmountReversalField.ne(BigDecimal.ZERO));
// insert for both cost-matched ACs and assumed_zero
context.insertInto(acar, acar.ACCRUED_COST_ALLOCATION_ID, acar.INVOICE_CURRENCY, acar.INVOICE_PAYMENT_FX_RATE, acar.INVOICE_PAYMENT_FX_RATE_PROVIDER,
acar.PAYMENT_CURRENCY, acar.PAYMENT_REPORTING_FX_RATE, acar.PAYMENT_REPORTING_FX_RATE_PROVIDER, acar.REPORTING_CURRENCY,
acar.EFFECTIVE_DATE, acar.VERSION, acar.CREATED, acar.LAST_MODIFIED,
acar.INVOICE_AMOUNT, acar.PAYMENT_AMOUNT, acar.REPORTING_AMOUNT)
.select(reversalSelect)
.execute();
Below is the query that is getting generated:
insert into `feed_history_reconciled_invoice_cost` (`feed_history_id`, `reconciled_invoice_cost_id`, `created`, `last_modified`, `version`)
select 3, `reconciled_invoice_cost`.`id`, timestamp with time zone '2024-02-12 17:11:40.056717925+00:00',
timestamp with time zone '2024-02-12 17:11:40.056717925+00:00', 1
from `reconciled_invoice_cost` join `invoice_line_item` on `
invoice_line_item`.`id` = `reconciled_invoice_cost`.`invoice_line_item_id` join `invoice` on `invoice_line_item`.`invoice_id` = `invoice`.`id`
join `tenant` on `invoice`.`tenant_id` = `tenant`.`id`
join `costing_entity` on `costing_entity`.`id` = `reconciled_invoice_cost`.`costing_entity_id`
left outer join `costing_entity` as `parentCe` on (`parentCe`.`id` = `costing_entity`.`parent_costing_entity_id` and `parentCe`.`entity_type` = ?)
where (`reconciled_invoice_cost`.`cost_type` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
and `reconciled_invoice_cost`.`retracted` = ? and `invoice`.`status` = ? and
trim(case when `costing_entity`.`entity_type` = ? then `parentCe`.`reference_03` else `costing_entity`.`reference_03` end) is not null
and ((`tenant`.`key` = ? and `reconciled_invoice_cost`.`erp_profit_center_code` in (?, ?, ?, ?)) or `tenant`.`key` = ?)
and `invoice_line_item`.`invoice_id` = ?)];
Â
Getting below error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'time zone '2024-02-12 17:11:40.056717925+00:00', timestamp with time zone '2024-' at line 1
at org.jooq_3.19.0.MYSQL.debug(Unknown Source)
Not sure what i am missing have been struggling with this with quite a few days. Any help will be appreciated.
Thanks,
Deepali Sharma