简体   繁体   English

休眠时间戳 - 毫秒精度

[英]Hibernate timestamp - millisecond precision

Seems like storing timestamps with millisecond precision is a know issue with hibernate. 似乎存储时间戳以毫秒精度是hibernate的一个已知问题。 My field in the db was initially set as timestamp(3), but I've tried datetime(3) as well...unfortunately, it didn't make any difference. 我在db中的字段最初被设置为timestamp(3),但我也尝试了datetime(3)......不幸的是,它没有任何区别。

I've tried using Timestamp and Date classes, and recently I've started using joda-time library. 我尝试过使用Timestamp和Date类,最近我开始使用joda-time库。 After all those efforts, I still wasn't unable to save timestamps with millisecond accuracy. 经过所有这些努力,我仍然无法以毫秒精度保存时间戳。

My mapping for the class contains following property: 我的类映射包含以下属性:

<property name="startTime" column="startTime"    type="org.jadira.usertype.dateandtime.joda.PersistentDateTime" length="3" precision="3" />

and I'v custom defined Dialect class 我是自定义Dialect类

public class MySQLQustomDialect extends MySQL5InnoDBDialect{
    protected void registerColumnType(int code, String name) {
        if (code == Types.TIMESTAMP) {
            super.registerColumnType(code, "TIMESTAMP(3)");
        } else {
            super.registerColumnType(code, name);
        }
    }
}

If I enter the data manually into db, hibernate manages to retrieve the sub second part. 如果我手动将数据输入db,hibernate会设法检索第二部分。

Is there any way to solve this issue? 有什么方法可以解决这个问题吗?

Are you, by any chance, using the MySQL Connector/J JDBC driver with MariaDB 5.5? 您是否有机会在MariaDB 5.5中使用MySQL Connector / J JDBC驱动程序?

Connector/J usually sends the milliseconds part to the server only when it detects that the server is new enough, and this detection checks that the server version is >= 5.6.4. Connector / J通常仅在检测到服务器足够新时才将毫秒部分发送到服务器,并且此检测检查服务器版本是否> = 5.6.4。 This obviously does not work correctly for MariaDB 5.5.x. 这显然不适用于MariaDB 5.5.x.

You can see the relevant part of Connector/J source here: 您可以在此处查看Connector / J源的相关部分:

http://bazaar.launchpad.net/~mysql/connectorj/5.1/view/head:/src/com/mysql/jdbc/PreparedStatement.java#L796 http://bazaar.launchpad.net/~mysql/connectorj/5.1/view/head:/src/com/mysql/jdbc/PreparedStatement.java#L796

Using MariaDB's own JDBC driver (MariaDB Java Client) might help (I haven't tried), but I accidentally discovered that adding useServerPrepStmts=true to the connection string makes this work with Connector/J, too. 使用MariaDB自己的JDBC驱动程序(MariaDB Java Client)可能有所帮助(我还没有尝试过),但我偶然发现将useServerPrepStmts = true添加到连接字符串使得这也适用于Connector / J.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM