Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing hibernate-mapped dates?
    primarykey
    data
    text
    <p>How can i map a date from a java object to a database with Hibernate? I try different approaches, but i am not happy with them. Why? Let me explain my issue. I have the following class [1] including the main method i invoke and with the following mapping [2]. The issue about this approach you can see, when you look at the console output.</p> <blockquote> <p>false </p> <p>false</p> <p>1</p> <p>-1</p> <p>1224754335648</p> <p>1224754335000 </p> <p>Thu Oct 23 11:32:15 CEST 2008</p> <p>Clock@67064</p> </blockquote> <p>As you can see the the to dates are not exactly equal, although they should, so it is hard to compare them without goofing around with return value of <code>getTime</code>. I also tried java.sql.Date, Timestamp and date instead of timestamp in the mapping, but without success. </p> <p>I wonder why the last three digits are zero and if this is a hibernate or a java issue or my own stupidity.</p> <p>Thank you for reading.</p> <p>[1]</p> <pre><code>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; } } </code></pre> <p>[2]</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="Clock" table="CLOCK"&gt; &lt;id name="id" column="CLOCK_ID"&gt; &lt;generator class="native"/&gt; &lt;/id&gt; &lt;property name="date" type="timestamp"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload