简体   繁体   中英

Hibernate timestamp - millisecond precision

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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