[英]Hibernate Temporal.TIMESTAMP maps to mysql datetime without millis
I have an entity with a field defined following way:我有一个实体,其字段定义如下:
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "some_datetime")
private java.util.Date someDateTime;
I would assume that Hibernate would create a table with corresponding column of type DATETIME(3)
(for MySQL), however the table's column is just a DATETIME
, so when I store a Date with milliseconds they are lost:我假设 Hibernate 会创建一个具有
DATETIME(3)
类型的相应列(对于 MySQL)的表,但是该表的列只是一个DATETIME
,所以当我存储一个带有毫秒的 Date 时,它们会丢失:
describe some_table;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| ... | ... | NO | PRI | NULL | |
| some_datetime | datetime | YES | | NULL | |
| ... | ... | NO | | NULL | |
+---------------+--------------+------+-----+---------+-------+
I guess I need to add a columnDefinition
parameter to the @Column
annotation, but why it's not done automatically by Hibernate, as I explicitly told that I want to have a TIMESTAMP supported by the table.我想我需要在
@Column
注释中添加一个columnDefinition
参数,但是为什么它不是由 Hibernate 自动完成的,因为我明确告诉我我想要一个表支持的 TIMESTAMP。
I tried the above with:我尝试了以上方法:
- 10.1.14 - MariaDB - 10.1.14 - MariaDB
- 5.7.29 - MySQL - 5.7.29 - MySQL
Hibernate version is 5.2.18.Final with MySQL5InnoDBDialect
Hibernate 版本是5.2.18.Final with
MySQL5InnoDBDialect
I would suggest you to use org.hibernate.dialect.MySQL57Dialect
dialect instead of org.hibernate.dialect.MySQL5InnoDBDialect
.我建议你使用
org.hibernate.dialect.MySQL57Dialect
方言而不是org.hibernate.dialect.MySQL5InnoDBDialect
。
As I see, the Types.TIMESTAMP
type redefined here as datetime(6)
正如我所见,
Types.TIMESTAMP
类型在这里重新定义为datetime(6)
public class MySQL57Dialect extends MySQL55Dialect {
public MySQL57Dialect() {
super();
registerColumnType( Types.TIMESTAMP, "datetime(6)" );
// ...
}
}
But MySQL5InnoDBDialect
inherited the Types.TIMESTAMP
declaration from the MySQLDialect
where it declared in the following way:但
MySQL5InnoDBDialect
继承了Types.TIMESTAMP
从声明MySQLDialect
它以下列方式宣告:
public class MySQLDialect extends Dialect {
public MySQLDialect() {
// ..
registerColumnType( Types.TIMESTAMP, "datetime" );
}
}
And, by the way, this dialect is marked as deprecated:而且,顺便说一句,这种方言被标记为已弃用:
/** A Dialect for MySQL 5 using InnoDB engine
* ...
* @deprecated Use "hibernate.dialect.storage_engine=innodb" environment variable or JVM system property instead.
*/
@Deprecated
public class MySQL5InnoDBDialect extends MySQL5Dialect {
// ...
}
It is not direct answer but if you in the beginning your developing process maybe it will be useful to use LocalDateTime instead of old Date object.这不是直接的答案,但如果您刚开始开发过程,使用LocalDateTime而不是旧的Date对象可能会很有用。 In this case it is no need use TemporalType.TIMESTAMP anymore.
在这种情况下,不再需要使用TemporalType.TIMESTAMP 。
https://www.baeldung.com/hibernate-date-time https://www.baeldung.com/hibernate-date-time
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.