0

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.

javatutorial
  • 1,916
  • 15
  • 20
  • What type of column is used in PostgreSQL - with or without timezone? Did you try to use global setting TimeZone.setDefault(TimeZone.getTimeZone("ETC/UTC"));? Similar question: http://stackoverflow.com/questions/22210570/unwanted-automatic-time-zone-conversion-using-hibernate-jpa-and-jdk-date . Related: http://stackoverflow.com/questions/508019/jpa-hibernate-store-date-in-utc-time-zone – Justinas Jakavonis Aug 26 '16 at 11:29
  • @Justas: it's a simple "date" column, not a "timestamp" column, so there's no "with/without timezone" option. I've also tried to set ETC/UTC as default time zone, with no success. As far as the other questions you linked, I had already read them, but my case is different because I have TemporalType.DATE whilst theirs is TemporalType.TIMESTAMP (in fact I have a "date" column on my Postgres, while those questions' are "timestamp"). Moreover, my problem is not a timezone one, but the fact that Hibernate retrieves different Dates from the SAME ROW, in a random manner... – javatutorial Aug 26 '16 at 13:30

0 Answers0