【问题标题】:Hibernate Annotation for TemporalType.TIMESTAMP PrecisionTemporalType.TIMESTAMP 精度的休眠注释
【发布时间】:2013-07-26 06:02:34
【问题描述】:

我正在使用 Java 中的 Hibernate for MySQL 数据库创建一个表。

其中一列是日期类型。

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

我知道可以在 MySQL 中使用指定 TIMESAMP 的精度

TIMESTAMP(6)

但是,您如何在 Hibernate 注释映射中做到这一点?我试过了

length = 19, precision = 38, scale = 20

似乎没有一个以毫秒为单位存储时间,也就是第二个 aka

1374839856000

相对

1374839855789

有人知道这个问题的解决方案吗?感谢您的帮助!

【问题讨论】:

  • 我认为您必须将列类型更改为 datetime 。
  • 我不相信有 TemporalType.DATETIME,Hibernate 中只有 DATE 或 TIME。
  • 这里有点混乱,1374839856000 需要你,但你会得到1374839855789。正确的?或者别的什么...
  • 不,我的意思是我想要 1374839855789,但是却得到 1374839856000

标签: java mysql database hibernate hibernate-annotations


【解决方案1】:

继承您在属性 hibernate.dialect 中配置的类,然后在那里更改使用的数据类型。将属性 hibernate.dialect 设置为您的新类。

这对我有用(从 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);
        }
    }
}

【讨论】:

  • 真的没有别的办法了吗?这看起来更像是一些解决方法。为什么这么难?时间戳精度在我看来是很常见的设置
猜你喜欢
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 2018-01-12
  • 2012-09-11
  • 1970-01-01
相关资源
最近更新 更多