Stefano Bossi
Stefano Bossi

Reputation: 1437

Jooq with POJO Converter

I am trying to implement a software to read and write data into an h2 database with the help of Jooq. My PLC_DATA Table has a column with TIMESTAMP which is normally mapped to LocalDateTime but I need this data to be mapped to Instant in my POJO, so I wrote my custom Converter:

public class TimestampConverter implements Converter<LocalDateTime, Instant> {

    private static final long serialVersionUID = -2866811348870878385L;

    /**
     * Convert a {@code LocalDateTime} into {@code Instant}
     */
    @Override
    public Instant from(LocalDateTime databaseObject) {
        return databaseObject.toInstant(ZoneOffset.UTC);
    }

    /**
     * Convert a {@code Instant} into {@code Timestamp}
     */
    @Override
    public LocalDateTime to(Instant userObject) {
        return userObject.atZone(ZoneOffset.UTC).toLocalDateTime();
    }

    /**
     * Return the from Type Class
     */
    @Override
    public Class<LocalDateTime> fromType() {
        return LocalDateTime.class;
    }

    /**
     * Return the to Type Class
     */
    @Override
    public Class<Instant> toType() {
        return Instant.class;
    }
}

and I have referenced the forcedType in my build.gradle file:

forcedType {
    userType = 'java.time.Instant'
    converter = 'it.fox.plcwebgui.utils.db.TimestampConverter'                      
    includeTypes = 'TIMESTAMP.*'
}

I have annotated my POJO:

public class PlcEventBean implements Serializable {

    private static final long serialVersionUID = 1988924276212981713L;

    @Column(name = "ID")
    public long id = 0;

    @Column(name = "EVENT_INSTANT")
    public Instant eventInstant = Instant.ofEpochMilli(0);

    @Column(name = "MAX_FORCE")
    private int maxForce;

    /**
     * Get the Event ID
     * @param id the id
     */
    public long getId() {
        return id;
    }

    /**
     * Set the Event ID
     * @param id the id
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * The instant (in GMT) of the event
     * @return the instant of the event
     */
    public Instant getEventDate() {
        return eventInstant;
    }

    /**
     * Set the instant of the Event
     * @param eventInstant the instant to set
     */
    public void setEventDate(Instant eventInstant) {
        this.eventInstant = eventInstant;
    }

    /**
     * The max Force used for the event
     * @return the max force used
     */
    public int getMaxForce() {
        return maxForce;
    }

    /**
     * Set the max force used for the Event
     * @param maxForce the value of the max force
     */
    public void setMaxForce(int maxForce) {
        this.maxForce = maxForce;
    }
}

The code work like a charm when I read from the DB to my POJO, like here:

public List<PlcEventBean> fetchData(int offset, int limit, DataFilter filter) {
    Instant fromInstant = filter.getFromInstant();
    Instant toInstant = filter.getToInstant();
    String id  = filter.getId();
    List<PlcEventBean> plcEventBeans = context.select()
        .from(PLC_DATA)
        .where(
            PLC_DATA.EVENT_INSTANT.greaterThan(fromInstant)
                                    .and(PLC_DATA.EVENT_INSTANT.lessThan(toInstant))
                                    .and(PLC_DATA.ID.like("%" + id + "%"))
            )
            .offset(offset)
            .limit(limit)
            .fetchInto(PlcEventBean.class);
    logger.info("Fetched {} with offset: {} limit: {} with fromDateTime {}, toDateTime {}, textSearch {}"
        ,plcEventBeans.size()
        ,offset
        ,limit
        ,fromInstant
        ,toInstant
        ,id
    );
    return plcEventBeans;
}

But when I try to write some data in the DB I got an exception trying to generate new records:

public void generateRandomValues() {
    int nEvents = 40000;
    Random r = new Random(0);
    List<PlcEventBean> plcEvents = new ArrayList<>();

    for (long i = 0; i < nEvents; i++) {
        PlcEventBean eventBean = new PlcEventBean();
        eventBean.setId(i);
        eventBean.setEventDate(Instant.now().plus(i, ChronoUnit.MINUTES));
        eventBean.setMaxForce(Math.abs(r.nextInt()));
        plcEvents.add(eventBean);
    }
    PlcDataRecord plcDataRecord = context.newRecord(PLC_DATA, plcEvents);
    context.executeInsert(plcDataRecord);
}
org.jooq.exception.DataTypeException: Cannot convert from it.fox.plcwebgui.plc.PlcEventBean@2a389173 (class it.fox.plcwebgui.plc.PlcEventBean) to class java.time.LocalDateTime
    at org.jooq.tools.Convert$ConvertAll.fail(Convert.java:1200)
    at org.jooq.tools.Convert$ConvertAll.from(Convert.java:1089)
    at org.jooq.tools.Convert.convert0(Convert.java:324)
    at org.jooq.tools.Convert.convert(Convert.java:316)
    at org.jooq.tools.Convert.convert(Convert.java:387)
    at org.jooq.impl.DefaultDataType.convert(DefaultDataType.java:827)
    at org.jooq.impl.ConvertedDataType.convert(ConvertedDataType.java:114)
    at org.jooq.impl.Tools.setValue(Tools.java:2823)
    at org.jooq.impl.DefaultRecordUnmapper$IterableUnmapper.unmap(DefaultRecordUnmapper.java:189)
    at org.jooq.impl.DefaultRecordUnmapper.unmap(DefaultRecordUnmapper.java:102)
    at org.jooq.impl.AbstractRecord.from0(AbstractRecord.java:837)
    at org.jooq.impl.AbstractRecord.from(AbstractRecord.java:867)
    at org.jooq.impl.DefaultDSLContext$6.operate(DefaultDSLContext.java:4019)
    at org.jooq.impl.RecordDelegate.operate(RecordDelegate.java:130)
    at org.jooq.impl.DefaultDSLContext.newRecord(DefaultDSLContext.java:4015)
    at it.fox.plcwebgui.plc.PlcEventServiceDatabaseImp.generateRandomValues(PlcEventServiceDatabaseImp.java:120)
    at it.fox.plcwebgui.utils.db.PlcEventServiceDatabaseImpTest.generateRandomValuesTest01(PlcEventServiceDatabaseImpTest.java:128)

As far as I understood in the documentation the Converter should work bidirectionally. What am I missing?

Regards,

Stefano

Upvotes: 2

Views: 464

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220762

The mistake is here:

PlcDataRecord plcDataRecord = context.newRecord(PLC_DATA, plcEvents);

You want to convert a list of pojos to a single record, which doesn't make sense. Move that logic into your loop, instead:

List<PlcDataRecord> records = new ArrayList<>();

for (long i = 0; i < nEvents; i++) {
    PlcEventBean eventBean = new PlcEventBean();
    eventBean.setId(i);
    eventBean.setEventDate(Instant.now().plus(i, ChronoUnit.MINUTES));
    eventBean.setMaxForce(Math.abs(r.nextInt()));
    records.add(context.newRecord(PLC_DATA, eventBean));
}

context.batchInsert(records);

Side note

Just rewrite your type to SQLDataType.INSTANT instead of rolling your own converter:

forcedType {
    name = 'INSTANT'
    includeTypes = 'TIMESTAMP.*'
}

Upvotes: 1

Related Questions