Hibernate timestamp - millisecond precision

倖福魔咒の 提交于 2019-12-12 12:13:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23142614/hibernate-timestamp-millisecond-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!