MySQL DATETIME precision (joda-time, Hibernate, org.jadira.usertype, hbm2ddl)

In my hibernate-4 object, I map the DateTime joda-time property using the recommended jadira usertypes :

@Entity
@Table(name="timing")
public class TimingEntity {
    ...
    @Basic(optional=false)
    @Column(name="moment")
    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    public DateTime getMoment() {
       ...

My database is MySQL. If the hibernate property is hbm2ddl.autoset to create, I have the following column generated in my table timing:

CREATE TABLE `timing` (
    ...
   `moment` DATETIME NOT NULL,
    ...
)

The generated one CREATE TABLEcontains a DATETIME column. DATETIME in MySQL has only second precision, without a fractional part. To enable the fractional part, before microseconds, MySQL 5.6.4 and higher includes DATETIME(precision) columns, for example DATETIME(3), to have milliseconds precision.

- , hbm2ddl? , jadira, java.sql jdbc ?

PS , , , , DATETIME(3), - joda DateTimes .

+4
2

@ # columnDefinition

@Basic(optional=false)
@Column(name="moment" columnDefinition="DATETIME(3) NOT NULL")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getMoment() 
{   
   ...

@ # , , .

+2

, MySQL @Column. , org.hibernate.dialect.MySQLDialect:

package org.yourproject;

import java.sql.Types;
import org.hibernate.dialect.MySQL5Dialect;

public class MySQL564PlusDialect extends MySQL5Dialect {
   public MySQL564PlusDialect() {
      super();
      registerColumnType( Types.TIMESTAMP, 6, "datetime($l)" );
   }
}

hibernate hibernate.dialect=org.yourproject.MySQL564PlusDialect (, , , , org.hibernate.dialect.MySQL5InnoDBDialect).

DATETIME @Column length:

@Basic(optional=false)
@Column(name="moment", length=3)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime getMoment() {
 ...

DATETIME(3), . DATETIME ( ), . length 6, .

, (, org.hibernate.dialect.MySQLDialect , , - ), : length @Column .

P.S. precision @Column length, "datetime($l)" "datetime($p)" .

+5

Source: https://habr.com/ru/post/1534429/


All Articles