Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle DateTime in JPA2

I'm using JPA2 with EclipseLink implementation. I'm simply trying to save the current Date into a DateTime column into a MySQL Database.

The date object which should be persisted is simply created:

import java.util.Date
Date currentDate = new Date();

Now the currentDate contains the exact date and time. This object is persisted in a table which has the following column:

@Column(name="DATE_CREATED")
@Temporal(TemporalType.DATE)
Date dateCreated;

The TemporalType has three constants:

  • DATE - this saves in the DB the date without any time: (2012-02-23 00:00:00)
  • TIME - this throws an incompatibility error
  • TIMESTAMP - this saves in the DB the date without any time: (2012-02-23 00:00:00)

The database column is created this way:

date_opening DATETIME NULL DEFAULT NULL,

For all this options I'm failing in saving the both the time and the date.

like image 982
Ionut Avatar asked Feb 23 '12 08:02

Ionut


People also ask

How does spring boot store date and time?

Use LocalDateTime and the appropriate date column type in the database. Then you will be able to easily do date math (e.g. comparison) both in a database query and in Java if necessary.

Does JPA support LocalDateTime?

Why does JPA not support LocalDate and LocalDateTime? The answer is simple, JPA 2.1 was released before Java 8 and the Date and Time API simply didn't exist at that point in time. Therefore the @Temporal annotation can only be applied to attributes of type java. util.

How do you use timestamp?

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.

Does JPA support instant?

JPA supports LocalDate , LocalTime , LocalDateTime etc. but not Instant.


2 Answers

This should work perfectly with TemporalType.TIMESTAMP and database column type DATETIME. Maybe you are checking type for wrong column: in mappings you have "DATE_CREATED" and in column definition "date_opening".

You asked also why there is no TemporalType.DATETIME. Reason is that values of TemporalType have one-to-one mapping to JDBC temporal types in java.sql.[DATE/TIME/TIMESTAMP], in the end JPA have to play together with JDBC.

I tested with following code (env: EclipseLink 2.3.0, Connector/J 5.1.6, MySQL 5.1):

Entity/mappings:

@Entity
public class SomeEntity {
    @Id
    private int id;

    @Column(name="DATE_CREATED")
    @Temporal(TemporalType.TIMESTAMP)
    private java.util.Date dateCreated;

    public SomeEntity(int id, Date dateCreated) {
        this.id = id;
        this.dateCreated = dateCreated;
    }
    public SomeEntity() {
    }
}

Table definition:

CREATE TABLE  `test`.`SOMEENTITY` (
  `ID` int(11) NOT NULL,
  `DATE_CREATED` datetime DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=innoDB DEFAULT CHARSET=utf8

Test:

java.util.Date now = new Date();
SomeEntity se = new SomeEntity(1, now);
em.persist(se);

It works as expected, also time part of DATE_CREATED is having correct value. If mismatch between columns was not the problem, maybe you can test this as well, and report results and MySQL and library versions.

like image 88
Mikko Maunu Avatar answered Oct 22 '22 18:10

Mikko Maunu


With the mapping :

@Temporal(TemporalType.TIMESTAMP)
    private java.util.Date dateCreated;

when you retrieve the data from MySQL - it gives something like :

1519621200000

like image 34
aakashpanjwani1 Avatar answered Oct 22 '22 19:10

aakashpanjwani1