我有一个Sqlite和下表:
CREATE TABLE foo (id bigint not null, from datetime, to datetime, (...) primary key (id));对应的hibernate-实体是:
@Entity
@Table(name = "foo")
public class Foo implements Serializable {
    @Id
    @Column(name = "id")
    private Long id;
    @Column(name = "from")
    private LocalDateTime from;
    @Column(name = "to")
    private LocalDateTime to;
    ...
}当持久化这样一个实体时,hibernate总是将LocalDateTime-字段插入为int (unix时间戳)。
有没有办法让hibernate将这些时间戳作为ISO-String插入?根据sqlite文档的说法,sqlite能够以下列一种格式存储日期,因此至少在技术上可以这样做:
SQLite没有预留用于存储日期和/或时间的存储类。相反,SQLite的内置日期和时间函数能够将日期和时间存储为文本、实值或整数值: 文本作为ISO8601字符串(“YYYY DD HH:MM:SS.SSS")。 真实的朱利安日数,从格林威治从公元前4714年11月24日正午起的天数,根据公历。 整数为Unix时间,自1970-01-01 00:00:00开始的秒数。
我已经尝试过重写SqliteDialect,但是它并没有改变任何东西:
public class MySqliteDialect extends SQLiteDialect {
    public MySqliteDialect() {
        super();
        registerColumnType( Types.TIMESTAMP, "text" );
    }
}tl;dr:如何将实体中的LocalDateTime字段存储在sqlite中的datetime列中作为ISO-string?
发布于 2021-03-24 17:11:22
不知道为什么只有很少的文档记录,但事实证明sqlite驱动程序中有一个属性控制着这种行为:

因此,解决我的问题的方法是在hibernate-properties中将data_class属性设置为TEXT:
properties.setProperty("hibernate.connection.date_class", "TEXT");https://stackoverflow.com/questions/66779817
复制相似问题