休眠时间戳 - 毫秒精度 [英] Hibernate timestamp - millisecond precision

查看:221
本文介绍了休眠时间戳 - 毫秒精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来像存储毫秒级精度的时间戳是hibernate的一个常见问题。
我在数据库中的字段最初设置为时间戳记(3),但我也尝试过日期时间(3),不幸的是,它没有任何区别。



我尝试过使用Timestamp和Date类,最近我开始使用joda时间库。经过所有这些努力,我仍然无法以毫秒级的精度节省时间戳。



这个类的映射包含以下属性:

 < property name =startTimecolumn =startTimetype =org.jadira.usertype.dateandtime.joda.PersistentDateTimelength =3precision =3/> 

和我自定义的Dialect类

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





如果我手动输入数据进入数据库,hibernate设法检索第二部分。



有什么方法可以解决这个问题吗?

解决方案

您是否有任何机会在MariaDB 5.5中使用MySQL Connector / J JDBC驱动程序?



Connector / J通常会发送仅当服务器检测到服务器足够新时,才会向服务器发送毫秒部分,并且此检测会检查服务器版本是否大于5.6.4。对于MariaDB 5.5.x,这显然无法正常工作。

您可以在这里看到Connector / J源代码的相关部分:

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



使用MariaDB自己的JDBC驱动程序( MariaDB Java客户端)可能会有所帮助(我还没有尝试过),但我意外地发现,将useServerPrepStmts = true添加到连接字符串中也可以使用Connector / J。


Seems like storing timestamps with millisecond precision is a know issue with 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.

I've tried using Timestamp and Date classes, and recently I've started using joda-time library. 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

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.

Is there any way to solve this issue?

解决方案

Are you, by any chance, using the MySQL Connector/J JDBC driver with MariaDB 5.5?

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. This obviously does not work correctly for MariaDB 5.5.x.

You can see the relevant part of Connector/J source here:

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.

这篇关于休眠时间戳 - 毫秒精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆