[英]Jooq with POJO Converter
我正在嘗試在 Jooq 的幫助下實現一個軟件來將數據讀取和寫入 h2 數據庫。 我的 PLC_DATA 表有一個包含 TIMESTAMP 的列,它通常映射到 LocalDateTime,但我需要將此數據映射到我的 POJO 中的 Instant,所以我編寫了自定義轉換器:
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;
}
}
我在我的build.gradle
文件中引用了 forcedType:
forcedType {
userType = 'java.time.Instant'
converter = 'it.fox.plcwebgui.utils.db.TimestampConverter'
includeTypes = 'TIMESTAMP.*'
}
我已經注釋了我的 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;
}
}
當我從數據庫讀取到我的 POJO 時,代碼就像一個魅力,就像這里一樣:
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;
}
但是當我嘗試在數據庫中寫入一些數據時,我在嘗試生成新記錄時遇到了異常:
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)
據我在文檔中的理解, Converter
應該雙向工作。 我錯過了什么?
問候,
斯特凡諾
錯誤在這里:
PlcDataRecord plcDataRecord = context.newRecord(PLC_DATA, plcEvents);
您想要將 pojo 列表轉換為單個記錄,這沒有意義。 將該邏輯移到您的循環中,而不是:
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);
只需將您的類型重寫為SQLDataType.INSTANT
而不是滾動您自己的轉換器:
forcedType {
name = 'INSTANT'
includeTypes = 'TIMESTAMP.*'
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.