title: Oracle getTime() keep milliseconds
body: <p>We have a Spring + Hibernate Java 7 web-application. We currently use <code>.hbm.xml</code> Hibernate files (instead of the more common known annotations) as mapping between the Java entities and database tables.</p>
  
  <p>One of our Java entity classes has a <code>java.util.Date</code> with millisecond precision:</p>
  
  <pre><code>public class SomeEntity extends AbstractEntity{
     ...
  
     private java.util.Date someDate; // with ms precision
  
     ...
  }
  </code></pre>
  
  <p>In our <code>.hbm.xml</code> file we have the following for this date:</p>
  
  <pre><code>...
  &lt;hibernate-mapping&gt;
    &lt;class name="com.namespace.Entity" table="entity" batch-size="10"&gt;
       ...
       &lt;property name="SomeDate" column="somedate" type="com.namespace.CustomDateType" /&gt;
       ...
  </code></pre>
  
  <p>Which is linked to a database table with the following row:</p>
  
  <pre><code>SOMEDATE  TIMESTAMP(2)  NULLABLE=Yes
  </code></pre>
  
  <p>Our <code>CustomDateType</code> class:</p>
  
  <pre><code>package com.namespace.type;
  
  import java.io.Serializable;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Timestamp;
  import java.sql.Types;
  import java.util.Date;
  
  import org.hibernate.usertype.UserType;
  
  public class CustomDateType implements UserType {
  
    private static final int[] SQL_TYPES = { Types.TIMESTAMP };
  
    @Override
    public Object assemble(final Serializable serializable, final Object o) {
      return deepCopy(serializable);
    }
  
    @Override
    public Object deepCopy(final Object value) {
      return value;
    }
  
    @Override
    public Serializable disassemble(final Object o) {
      return (Serializable) deepCopy(o);
    }
  
    @Override
    public boolean equals(final Object x, final Object y) {
      if (x == y) {
        return true;
      } else if ((x == null) || (y == null)) {
        return false;
      }
      return x.equals(y);
    }
  
    @Override
    public int hashCode(final Object o) {
      return ((CustomDateType) o).hashCode();
    }
  
    @Override
    public boolean isMutable() {
      return false;
    }
  
    @Override
    public Object nullSafeGet(final ResultSet resultSet, final String[] names, final Object owner) throws SQLException {
      Date result = null;
      final Timestamp timestamp = resultSet.getTimestamp(names[0]); // This is where the problem is
      if (!resultSet.wasNull()) {
        result = new Date(timestamp.getTime());
      }
      return result;
    }
  
    @Override
    public void nullSafeSet(final PreparedStatement statement, final Object value, final int index) throws SQLException {
      final Date dateToSet = (Date) value;
      statement.setTimestamp(index, dateToSet == null ? null : new Timestamp(dateToSet.getTime()));
    }
  
    @Override
    public Object replace(final Object o, final Object o1, final Object o2) {
      return o;
    }
  
    @Override
    public Class&lt;Date&gt; returnedClass() {
      return Date.class;
    }
  
    @Override
    public int[] sqlTypes() {
      return SQL_TYPES;
    }
  }
  </code></pre>
  
  <p>When debugging <code>someDate</code> does indeed have milliseconds in the Java class. But, in the <code>CustomDateType#nullSafeGet</code> method the resulting <code>TimeStamp</code> doesn't have any milliseconds anymore.</p>
  
  <p>The <code>resultSet</code> parameter is the <code>oracle.jdbc.driver.ForwardOnlyResultSet</code>, which continues into <code>oracle.jdbc.driver.T4CPreparedStatement</code>, which continues into the class &amp; method where the core of the problem is: <code>oracle.jdbc.driver.DateTimeCommonAccessor#getTime(int, Calendar)</code></p>
  
  <p>In this method the <code>milliseconds</code> are stripped and you are left with <code>second</code>-precision.</p>
  
  <p>Is there any way to keep the milliseconds in this situation? How to save millisecond precision when going from <code>java.util.Date</code> to database <code>TIMESTAMP</code>-typed column?</p>
  
  <p>NOTE: The milliseconds are already gone before going into the database table. If I put a breakpoint at <code>final Timestamp timestamp = resultSet.getTimestamp(names[0]);</code> and step over, the resulting <code>timestamp</code> will have no milliseconds anymore.</p>
  
  <hr>
  
  <p>EDIT: The contents of the <code>hava.util.Date</code> object and <code>java.sql.Timestamp</code> objects during debugging:</p>
  
  <p><a href="https://i.stack.imgur.com/Z4yEB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z4yEB.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/s1toQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s1toQ.png" alt="enter image description here"></a></p>
  
  <p>The <code>fraction</code> and <code>millis</code> are both removed and put to <code>0</code>..</p>