Comparison of hibernation dates?

How can I match the date from a java object to a database with Hibernate? I try different approaches, but I am not happy with them. What for? Let me explain my problem. I have the following class [1], including the main method that I call, and with the following mapping [2]. You can see a question about this approach when you look at the console output.

false

falsely

1

1

1224754335648

1224754335000

Thu Oct 23 11:32:15 CEST 2008

Watch @ 67064

As you can see, date dates are not exactly equal, although they should, so they are difficult to compare without resorting to the return getTime value. I also tried java.sql.Date, Timestamp and date instead of the timestamp in the mapping, but to no avail.

I wonder why the last three digits are zero, and if it is sleep mode or a problem with java or my own stupidity.

Thanks for reading.

[1]

 public class Clock { int id; java.util.Date date; public static void main(String[] args) { HibernateUtil.init(); HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); Clock clock = new Clock(); clock.date = new java.util.Date(); HibernateUtil.getSessionFactory().getCurrentSession().saveOrUpdate(clock); HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit(); HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction(); Clock fromDBClock = (Clock)HibernateUtil.getSessionFactory() .getCurrentSession().get(Clock.class, 1); System.out.println(clock.date.equals(fromDBClock.date)); System.out.println(fromDBClock.date.equals(clock.date)); System.out.println(clock.date.compareTo(fromDBClock.date)); System.out.println(fromDBClock.date.compareTo(clock.date)); System.out.println(clock.date.getTime()); System.out.println(fromDBClock.date.getTime()); System.out.println(clock.date.toString()); System.out.println(fromDBClock.toString()); HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit(); HibernateUtil.end(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public java.util.Date getDate() { return date; } public void setDate(java.util.Date date) { this.date = date; } } 

[2]

 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Clock" table="CLOCK"> <id name="id" column="CLOCK_ID"> <generator class="native"/> </id> <property name="date" type="timestamp"/> </class> </hibernate-mapping> 
+4
source share
4 answers

The accuracy of DateTime MySql is only the second. Java date precision is millisecond. This is why the last three digits are zeros after they have been placed in the database.

Do this with your original date:

date = date.setTime ((date.getTime () / 1000) * 1000);

This will set it to the last exact second, and all your comparisons will match.

(BTV, System.out.println (fromDBClock.toString ()); should be System.out.println (fromDBClock.date.toString ());

+3
source

SQL Server stores the date and time with an accuracy of 3 milliseconds, which can definitely cause the problem you are seeing. One consequence of this is that 23: 59: 59.999 in Java ends the next day in SQL Server. I have never had a problem with Oracle, Informix or MySQL, but other databases may have less accuracy. You can get around this using custom Hibernate types. You should round to something less accurate than the base accuracy of the database when you write out the date values. Locate UserType in the Hibernate documentation, you will change the nullSafeSet method for rounding.

+2
source

Apparently, the TIMESTAMP type on your target RDBMS (what are you using, by the way?) Does not store milliseconds. Try to find another native type that does, or match your time with something else, such as long = ms-since-epoch.

+1
source

Personnaly, I truncate every date that I get in my POJO object, with the Apache commons lang package class named DateUtils.

See [Apons commons site] [1]

[1]: http://commons.apache.org/lang/api/org/apache/commons/lang/time/DateUtils.html#truncate(java.util.Date , int)

0
source

All Articles