Reputation: 33
I'm trying to store some time information in MySQL database with millisecond precision. The format that I want to store is 2015-08-13 10:56:24.570
like that.
The problem is, I have to create table column with length attributes (3 or 6, depends on fractional part). The application that I wrote with Java Spring Framework automatically creates mysql table if it isn't created before. But I can't set the length attributes of date field.
"@Column
annotation has the length argument but it only considerable with String fields." says the document of Spring Jpa.
Another interesting part is I couldn't use @Temporal
annotation. My IDE gives an error that "The annotation @Temporal
is disallowed for this location". I wonder why I given this error also.
Any help would be great for me.
EDIT: My Java code for creating mysql table
@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.
}
Upvotes: 3
Views: 8553
Reputation: 2177
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.
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:
import org.springframework.data.jpa.repository.Temporal;
I changed the import to the following, and the error went away:
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;
}
Upvotes: 0
Reputation: 5649
Make the column of type
java.sql.Timestamp
while storing the time related information in 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.
Upvotes: 1