MySQL DATETIME精度(joda-time,Hibernate,org.jadira.usertype,hbm2ddl) [英] MySQL DATETIME precision (joda-time, Hibernate, org.jadira.usertype, hbm2ddl)

查看:352
本文介绍了MySQL DATETIME精度(joda-time,Hibernate,org.jadira.usertype,hbm2ddl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的hibernate-4实体中,我绘制了一个joda时间的nofollow> DateTime 属性:

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

我的数据库是MySQL。使用hibernate属性 hbm2ddl.auto 设置为 create 值,我在定时表:

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

生成的 CREATE TABLE 包含DATETIME列。 MySQL中的DATETIME只有几秒精度,没有小数部分。为了启用小数部分,高达微秒,MySQL 5.6.4及更高版本启用 DATETIME(精度)列,例如 DATETIME(3)以具有毫秒的精度。



我的问题是 - 有没有办法指定hbm2ddl生成的时态字段的精度?至少,这是jadira usertypes,java.sql还是jdbc驱动程序机制的问题?



PS 当我手动修改数据库表具有我想要的精确列精度,例如 DATETIME(3),一切正常 - joda DateTimes以毫秒精度从DB读取并读取。

解决方案

使用可以使用

<$ p $ Column.html#columnDefinition%28%29rel =nofollow> @ Column#columnDefinition

@Column(name =momentcolumnDefinition =DATETIME(3)NOT NULL)
@Type(type =org。 jadira.usertype.dateandtime.joda.PersistentDateTime)
public DateTime getMoment()
{
...

您也可以尝试 <强> @Colum n#precision 属性,但是在文档中写明这只适用于小数。


In my hibernate-4 entity, I am mapping a joda-time DateTime 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. With hibernate property hbm2ddl.auto set to create value, I have the following column generated in my timing table:

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

The generated CREATE TABLE contains the DATETIME column. The DATETIME in MySQL has only seconds precision, without fractional part. In order to enable fractional part, up to microseconds, MySQL 5.6.4 and higher enables DATETIME(precision) columns, for example DATETIME(3) to have milliseconds precision.

My question is -- is there way to specify precision for my temporal fields generated with hbm2ddl? At least, is this a matter of jadira usertypes, or java.sql, or jdbc driver machinery?

P.S. When I manually modify the DB table to have the exact column precision I want, say, DATETIME(3), everything works OK - joda DateTimes are written and read from the DB with milliseconds precision.

解决方案

Use can use @Column#columnDefinition property for it

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

Also you can try @Column#precision property, but in documentation is written that this working only for decimals.

这篇关于MySQL DATETIME精度(joda-time,Hibernate,org.jadira.usertype,hbm2ddl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆