Reputation: 5850
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
Upvotes: 4
Views: 914
Reputation: 13041
I would suggest you to use org.hibernate.dialect.MySQL57Dialect
dialect instead of org.hibernate.dialect.MySQL5InnoDBDialect
.
As I see, the Types.TIMESTAMP
type redefined here as 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:
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 {
// ...
}
Upvotes: 4
Reputation: 141
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. In this case it is no need use TemporalType.TIMESTAMP anymore.
https://www.baeldung.com/hibernate-date-time
Upvotes: 2