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 errorTIMESTAMP– 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.
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:
Table definition:
Test:
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.