Reputation: 5316
I have a PostgreSQL table containing a timestamp without time zone
field. This is configured in my Java code as follows:
@Column(name = "timestamp")
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date timestamp;
The timestamp property is being set to new Date()
i.e. containing both the date and the time. However in the database the date part is not being persisted e.g. "14:40:28.889"; therefore when it is retrieved from the database the date is not loaded and set to default i.e. 01/01/1970. Any ideas where the date part of the timestamp is running off to?
UPDATE: I increased the logging level for hibernate so I can see what is being persisted by adding the following in the application.properties file:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
The log output was as follows:
binding parameter [6] as [TIMESTAMP] - [Wed Sep 16 08:57:03 CEST 2015]
UPDATE 2:
I stepped through the code and found the insert sql with the date formatted as 2015-09-16 11:16:09.416000 +02:00:00
. I insert the string automatically in PGAdmin and it insert with no error. After save a clicked the little refresh button and the date part disappeared! So the data is being persisted correctly but somehow postgre is doing something weird afterwards.
Upvotes: 2
Views: 997
Reputation: 5316
This was one of the silliest mistakes ever, I had a script that creates the tables and it was marked as "time without timezone" i.e. not timestamp so PostgreSQL was obviously removing the date. I will leave this question as it might help someone debug similar issues.
Upvotes: 2