I am trying to use Java Jooq library for inserting a timestamp with millisecond precision in a MariaDB table where I have a column of type TIMESTAMP(6). The line of code, just as an example is:
eventRecord.setEventtimestamp(LocalDateTime.ofEpochSecond(Instant.parse("2021-05-18T16:47:31.862750Z").getEpochSecond() , Instant.parse("2021-05-18T16:47:31.862750Z").getNano(), ZoneOffset.UTC));
Unfortunately the result in MariaDB is truncated and no milliseconds are inserted:
2021-05-18 16:47:31.000000
The table structure is:
MariaDB [plc_data]> DESCRIBE events;
+---------------------+--------------+------+-----+------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+------------------+-------+
| event_id | bigint(20) | NO | PRI | NULL | |
| eventTimestamp | timestamp(6) | YES | | NULL | |
| insertTimestamp | timestamp(6) | YES | | utc_timestamp(6) | |
| machineSerialNumber | int(11) | NO | | NULL | |
| data_block | longtext | NO | | NULL | |
+---------------------+--------------+------+-----+------------------+-------+
And the generated code for the two timestamp fields:
/**
* The column <code>plc_data.events.eventTimestamp</code>. GMT
*/
public final TableField<EventsRecord, Instant> EVENTTIMESTAMP = createField(DSL.name("eventTimestamp"), org.jooq.impl.SQLDataType.LOCALDATETIME.defaultValue(org.jooq.impl.DSL.field("NULL", org.jooq.impl.SQLDataType.LOCALDATETIME)), this, "GMT", new TimestampConverter());
/**
* The column <code>plc_data.events.insertTimestamp</code>. GMT
*/
public final TableField<EventsRecord, Instant> INSERTTIMESTAMP = createField(DSL.name("insertTimestamp"), org.jooq.impl.SQLDataType.LOCALDATETIME.defaultValue(org.jooq.impl.DSL.field("utc_timestamp(6)", org.jooq.impl.SQLDataType.LOCALDATETIME)), this, "GMT", new TimestampConverter());
The TimestampConverter class you could see in the generated code is a custom converter I wrote:
* Jooq converter to use Instant instead of SQL TIMESTAMP which is mapped
* into LocalDateTime
*/
public class TimestampConverter implements Converter<LocalDateTime, Instant> {
private static final long serialVersionUID = -2866811348870878385L;
/**
* Convert from {@code LocalDateTime} to {@code Instant}
*/
@Override
public Instant from(LocalDateTime databaseObject) {
return databaseObject.atZone(ZoneOffset.UTC).toInstant();
}
/**
* Convert from {@code Instant} to {@code Timestamp}
*/
@Override
public LocalDateTime to(Instant userObject) {
return userObject.atZone(ZoneOffset.UTC).toLocalDateTime();
}
/**
* Return the from Type Class (Database Type Class)
*/
@Override
public Class<LocalDateTime> fromType() {
return LocalDateTime.class;
}
/**
* Return the to Type Class (User type Class)
*/
@Override
public Class<Instant> toType() {
return Instant.class;
}
}
Which is the way to instruct Jooq to insert the milliseconds part of my date ?
Regards,
S.