Closed sunnyfun888 closed 3 years ago
Thanks for report. We need refactor our tests to cover more cases
I tried to fix the issue and here is the code for reference:
In DataTypeDateTime.java, change the method public void serializeBinary(Object data, BinarySerializer serializer) throws SQLException, IOException to:
private ZoneId zid = ZoneId.systemDefault();
@Override
public void serializeBinary(Object data, BinarySerializer serializer) throws SQLException, IOException {
ZonedDateTime zdt = null;
if (data instanceof LocalDateTime) {
zdt = ((LocalDateTime) data).atZone(zid);
} else if (data instanceof ZonedDateTime) {
zdt = (ZonedDateTime) data;
}
if (zdt != null) {
serializer.writeInt((int) DateTimeUtil.toEpochSecond(zdt));
}
}
In class AbstractPreparedStatement.java, change the method private boolean assembleSimpleParameter(StringBuilder queryBuilder, Object parameter) to:
protected ZoneId zid = ZoneId.systemDefault();
private boolean assembleSimpleParameter(StringBuilder queryBuilder, Object parameter) {
if (parameter == null) {
return assembleWithoutQuotedParameter(queryBuilder, "Null");
} else if (parameter instanceof Number) {
return assembleWithoutQuotedParameter(queryBuilder, parameter);
} else if (parameter instanceof String) {
return assembleQuotedParameter(queryBuilder, String.valueOf(parameter));
} else if (parameter instanceof LocalDate) {
return assembleQuotedParameter(queryBuilder, dateFmt.format((LocalDate) parameter));
} else if (parameter instanceof ZonedDateTime) {
return assembleQuotedParameter(queryBuilder, timestampFmt.format((ZonedDateTime) parameter));
} else if (parameter instanceof LocalDateTime) {
return assembleQuotedParameter(queryBuilder, timestampFmt.format(((LocalDateTime) parameter).atZone(zid)));
}
return false;
}
This will fix the insert and update issue for LocalDateTime, but not consider other date types eg. java.util.Date.
It works, but I'm afraid we can't do it in this way. We are refactoring to make all IDataType
s clean and move all type cast stuffs to JDBC relative classes.
close by #316
Environment
Error logs
Steps to reproduce
Create an entity class with attribute type java.time.LocalDateTime, then try to use spring JdbcTemplate to do the insert.
Other descriptions
It seems DataTypeDateTime.java is trying to cast any object to java.time.ZonedDateTime, then Caused the java.lang.ClassCastException.