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:
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.
I tried the above with:
- 10.1.14 - MariaDB
- 5.7.29 - MySQL
Hibernate version is 5.2.18.Final with MySQL5InnoDBDialect
I would suggest you to use org.hibernate.dialect.MySQL57Dialect
dialect instead of org.hibernate.dialect.MySQL5InnoDBDialect
.
As I see, the Types.TIMESTAMP
type redefine here as datetime(6)
public class MySQL57Dialect extends MySQL55Dialect {
public MySQL57Dialect() {
super();
registerColumnType( Types.TIMESTAMP, "datetime(6)" );
// ...
}
}
but MySQL5InnoDBDialect
extends MySQL5Dialect
that extends MySQLDialect
where Types.TIMESTAMP
declared as
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 {
// ...
}