12

In our DB we have multiple entities with Date fields. Oracle sees every date as the same, with a date and a time part. JPA entities however distinguish via the annotaton @Temporal. When we want to omit the time part we annotate Date fields with @Temporal(TemporalType.DATE) and Oracle saves 00:00:00, if not, we just leave it without annotation.

Example:

@Entity
public class MyEntity implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  private long myentityId;

  @Temporal(TemporalType.DATE)
  private Date importantDate; //01.01.2015 00:00:00

  private Date creationDate; //01.01.2015 10:35:51
  ...
}

...
MyEntity me = new MyEntity();
me.setImportantDate(new Date());
me.setCreationDate(new Date());
...

We upgraded from Oracle 11 to Oracle 12 and now the time part of importantDate is no longer omitted!

I tested this extensively on both databases with exactly the same program. This actually breaks our application.

What can I do to restore the previous behaviour?


UPDATE 1: I narrowed the problem down: driver ojdbc6 12.1.0.1.0 has the problem, ojdbc6 11.2.0.3.0 works as intended. (both using an Oracle 12 DB)

Is this a continuation of the timestamp problem fixed in 11.1? (http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_01)


UPDATE 2: Since Hibernate does not seem to be the problem, I wrote an example with pure JDBC:

OracleDataSource ods = new OracleDataSource();
...
Connection conn = ods.getConnection();
PreparedStatement ps = conn.prepareStatement("UPDATE MyEntity SET importantDate = ? WHERE myentityId = 4385");
ps.setDate(1, new java.sql.Date(new java.util.Date().getTime()));
ps.execute();
...

This snippet behaves different when switching between ojdbc6 11.1 and ojdbc6 12.1.

9
  • Have you updated the Oracle Driver as well in your app ?
    – Gaël J
    Commented Oct 20, 2015 at 9:03
  • 1
    Which version of Hibernate are you using? Are you using the Oracle12cDialect?
    – Tunaki
    Commented Oct 20, 2015 at 9:03
  • We still use org.hibernate.dialect.Oracle10gDialect and OJDBC7 driver. Will try new dialect now.
    – Thomas
    Commented Oct 20, 2015 at 9:19
  • One does wonder why Hibernate cannot auto-detect what RDBMS is being used and pick the right "dialect". After all, other JPA providers can do that Commented Oct 21, 2015 at 14:52
  • 1
    @Thomas6767: We tried to install some Oracle WL patches. Didn't work either. There's a workarround explained here: stackoverflow.com/questions/10429276/…. Although this isn't a very satisfying solution. Did you consider an Oracle Service Request?
    – themenace
    Commented Oct 29, 2015 at 14:36

1 Answer 1

8

We have conntacted the Oracle Support and they replied as follows (unfortunately I'm not able to provide link to the answer because an Oracle Support Account is needed):

The new behaviour works as intended:

In JDBC 12.1.0.1, getDate and setDate do not truncate the time component of the date. This behavior is different to JDBC 11.2.0.X, where the time component is truncated. As per bug 14389749, 17228297 this change is deliberate and the 12c driver's behavior is correct.

The workarounds provided work for me:

Workaround #1: Modify application to not insert the time component (e.g. with a static UtilMethod)

public static Date truncateTime(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); }

Workaround #2: Download and apply patch 19297927 from MOS: (My Oracle Support)

  1. Click on Patches & Updates tab.
  2. Enter the above patch number and Click on Search.
  3. Click on the patch number corresponds to your platform from the list
  4. Click on Download button to download the patch.
  5. Read any applicable notes before downloading, then click the Download button.

After patching replace ojdb7.jar in %Oracle_Home%\oracle_common\modules\oracle.jdbc_12.1.0 and add -Doracle.jdbc.DateZeroTime=true to your JVM Arguments

1
  • Thanks for the details. When Oracle support told us, the behaviour is as intended and Apache's Hibernate team didn't respond, we implemented the workaround with triggers to be save in the future.
    – Thomas
    Commented Nov 16, 2015 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.