简体   繁体   English

Java Spring - MySQL时间戳字段和毫秒精度

[英]Java Spring - MySQL timestamp field and millisecond precision

I'm trying to store some time information in MySQL database with millisecond precision. 我试图以毫秒精度在MySQL数据库中存储一些时间信息。 The format that I want to store is 2015-08-13 10:56:24.570 like that. 我要存储的格式是2015-08-13 10:56:24.570

The problem is, I have to create table column with length attributes (3 or 6, depends on fractional part). 问题是,我必须创建具有长度属性的表列(3或6,取决于小数部分)。 The application that I wrote with Java Spring Framework automatically creates mysql table if it isn't created before. 我用Java Spring Framework编写的应用程序会自动创建mysql表,如果它之前没有创建的话。 But I can't set the length attributes of date field. 但我无法设置日期字段的长度属性。

" @Column annotation has the length argument but it only considerable with String fields." @Column注释具有长度参数,但只有String字段才有。” says the document of Spring Jpa. 春天Jpa的文件说。

Another interesting part is I couldn't use @Temporal annotation. 另一个有趣的部分是我无法使用@Temporal注释。 My IDE gives an error that "The annotation @Temporal is disallowed for this location". 我的IDE给出了一个错误:“此位置不允许注释@Temporal ”。 I wonder why I given this error also. 我想知道为什么我也给出了这个错误。

Any help would be great for me. 任何帮助对我来说都很棒。

EDIT: My Java code for creating mysql table 编辑:我的Java代码用于创建mysql表

@Entity
@Table(name = "dbTable")
public class Report
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "myDate", nullable = false, length = 6)
    private java.util.Date date;

    // Getters Setters etc.

}

Make the column of type 制作类型的列

java.sql.Timestamp 的java.sql.Timestamp

while storing the time related information in MySQL. 同时在MySQL中存储与时间相关的信息。

Simple Example:- 简单示例: -

  public static void main(String[] args){
    System.out.println(java.sql.Timestamp.valueOf("2013-10-10 10:49:29.12000"));
}

Output:- 2013-10-10 10:49:29.12

Read this , if it helps. 阅读本文 ,如果有帮助的话。

Change the column type to java.sql.Timestamp from java.util.Timestamp. 将列类型从java.util.Timestamp更改为java.sql.Timestamp。

I realize this is kind of an old post, but I had the same problem and figured out a way around it. 我意识到这是一个很老的帖子,但我遇到了同样的问题,想出了解决方法。 Here's what my entity looked like: 这是我的实体看起来像:

@Entity
@Table(name = "ActivityLog")
public class ActivityLogBean {
    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    private java.util.Date alDateTime;
}

I was getting the error message "The annotation @Temporal is disallowed for this location" on the "@Temporal(TemporalType.TIMESTAMP)" line. 我在“@Temporal(TemporalType.TIMESTAMP)”行上收到错误消息“对此位置禁止注释@Temporal”。

The problem had to do with which Temporal class I was importing, and the import statement that I was using when I had the problem was: 问题与我导入的Temporal类有关,当我遇到问题时我使用的import语句是:

import org.springframework.data.jpa.repository.Temporal; import org.springframework.data.jpa.repository.Temporal;

I changed the import to the following, and the error went away: 我将导入更改为以下内容,错误消失了:

import javax.persistence.Temporal; import javax.persistence.Temporal;

Here's my full working class now: 这是我现在的全职工作班:

package com.mypackage.beans;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "ActivityLog")
public class ActivityLogBean {
    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    private java.util.Date alDateTime;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM