I am retrieving a bunch of objects containing a Date field using HQL. The problem is that Hibernate returns a different java.util.Date each time i run my process.
This is a simplified version of my entity:
@Entity
@Table(name = "MY_TABLE")
publuc class MyEntity implements Serializable {
@Id
@Column(name = "ID")
private String id;
@Temporal(TemporalType.DATE)
@Column(name = "MY_DATE", nullable = true)
private Date myDate;
// other fields...
// getters and setters
}
The "MY_DATE" column on Postgres is a simple "date" column (so, no timestamp).
This is the method I'm using to retrieve the objects:
@SuppressWarnings("unchecked")
private List<MyEntity> retrieveMyEntities(List<String> idsToRetrieve)
{
Query query = session.createQuery("from MyEntity i where i.id in (:params)");
query.setParameterList("params", idsToRetrieve);
return query.list();
}
I take care of opening and closing the session outside this method call.
Each time I run my process, I get Dates that differ from what I actually see on the database (but this might be due to a different timezone setting, so I don't really care), and that differ from each process run (and this is alarming...).
Here is an example, taking a single object as a test case. I run the process 3 times.
First process run:
- date that I see on DB, row XYZ: 1945-05-24
- retrieve using
retrieveMyEntities
... - date.getTime() = -776570400000, corresponding to Wed May 23 22:00:00 UTC 1945
Second process run:
- date that I see on DB, same row XYZ: 1945-05-24 (as before)
- retrieve using
retrieveMyEntities
... - date.getTime() = -776574000000, corresponding to Wed May 23 21:00:00 UTC 1945 (different from the previous process run!)
Third process run:
- date that I see on DB, same row XYZ: 1945-05-24 (as before)
- retrieve using
retrieveMyEntities
... - date.getTime() = -776570400000, corresponding to Wed May 23 22:00:00 UTC 1945 (different from the previous process run, but the same as the 1st run!)
TL;DR
Why does my process retrieve different Dates from DB each time it runs? I'm using PostgreSQL 9.4, Hibernate 5 and Java 1.7.