简体   繁体   中英

Hibernate Annotation for TemporalType.TIMESTAMP Precision

I'm creating a table using Hibernate for MySQL database in Java.

One of the columns is of Date type.

@Temporal( TemporalType.TIMESTAMP )
@Column( name = "event_start_time", nullable = false, length = 19 )
public Date getEventStartTime()
{
    return eventStartTime;
}

I know that it's possible to specify the precision of TIMESAMP in MySQL using

TIMESTAMP(6)

However, how do you do that in Hibernate annotation mapping? I've tried

length = 19, precision = 38, scale = 20

none of which seem to store the time in milliseconds past the second aka

1374839856000

as opposed to

1374839855789

Anyone know a solution to this? Thanks for the help!

Subclass the class you configured in property hibernate.dialect and then change the used data type there. Set the property hibernate.dialect to your new class.

This is what worked for me (since MySQL 5.6.4):

public class MySQL564InnoDBDialect extends MySQL5InnoDBDialect {

    protected void registerColumnType(int code, String name) {
        if (code == Types.TIMESTAMP) {
            super.registerColumnType(code, "TIMESTAMP(6)");
        } else {
            super.registerColumnType(code, name);
        }
    }
}

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