NutzCN Logo
问答 时隔2000天后,再次提问:关于localdatetime存储的问题
发布于 1185天前 作者 qq_c0a91281 1613 次浏览 复制 上一个帖子 下一个帖子
标签:

实体:

@Table("dim_channel_copy")
public class DimChannel {
 @Column(hump = true, value = "date")
 private LocalDateTime localDateTime;
...setget
}

Dao:

NutDao nutDao = new NutDao(dataSource);
DimChannel dimChannel = new DimChannel();
dimChannel.setLocalDateTime(LocalDateTime.now());
nutDao.insert(dataSource);

报错:

Exception in thread "main" org.nutz.dao.DaoException: org.nutz.castor.FailToCastObjectException: Fail to cast 'Wed Aug 18 18:27:02 CST 2021' to <long>
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:140)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:93)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:82)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
	at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:56)
	at org.nutz.dao.impl.NutDao.insert(NutDao.java:149)
	at org.myorg.quickstart.dim.DimChannel.main(DimChannel.java:72)
Caused by: org.nutz.castor.FailToCastObjectException: Fail to cast 'Wed Aug 18 18:27:02 CST 2021' to <long>
	at org.nutz.castor.castor.String2Number.cast(String2Number.java:45)
	at org.nutz.castor.castor.String2Number.cast(String2Number.java:19)
	at org.nutz.castor.Castors.cast(Castors.java:253)
	at org.nutz.castor.Castors.castTo(Castors.java:317)
	at org.nutz.dao.jdbc.Jdbcs$Adaptor$7.set(Jdbcs.java:420)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:306)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:90)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.DaoLogInterceptor.filter(DaoLogInterceptor.java:22)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.DaoInterceptorChain.invoke(DaoInterceptorChain.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.runCallback(NutDaoRunner.java:159)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:126)
	... 7 more
5 回复

加一个转换器吧,反正都jdk8了,最低

欧克,我看看转换器

是这意思哈?
哪我是不是需要把 LocalDatetime LocalDate LocalTime 都给写了?

public class LocalDatetimeCastor extends Castor<LocalDateTime, Long> {

    @Override
    public Long cast(LocalDateTime src, Class<?> toType, String... args) throws FailToCastObjectException {
        return src.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

}

不是那个转换器,应该是这个转换器

       Jdbcs.register(LocalDate.class.getName(), new ValueAdaptor() {
            public Object get(ResultSet rs, String colName) throws SQLException {
                Timestamp ts = rs.getTimestamp(colName);
                return null == ts ? null : LocalDate.from(ts.toInstant());
            }

            public void set(PreparedStatement stat, Object obj, int i) throws SQLException {
                if (null == obj) {
                    stat.setNull(i, Types.TIMESTAMP);
                } else {
                    stat.setDate(i, Date.valueOf(((LocalDate) obj)));
                }
            }
        });

        Jdbcs.register(LocalTime.class.getName(), new ValueAdaptor() {
            public Object get(ResultSet rs, String colName) throws SQLException {
                Timestamp ts = rs.getTimestamp(colName);
                return null == ts ? null : LocalTime.from(ts.toInstant());
            }

            public void set(PreparedStatement stat, Object obj, int i) throws SQLException {
                if (null == obj) {
                    stat.setNull(i, Types.TIMESTAMP);
                } else {
                    stat.setTime(i, Time.valueOf(((LocalTime) obj)));
                }
            }
        });
添加回复
请先登陆
回到顶部