HHH-8517 strip time from Date descriptors

Conflicts:
	hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CalendarTimeTypeDescriptor.java
This commit is contained in:
Brett Meyer 2013-09-19 15:22:14 -04:00
parent 1d53ebb120
commit 24a36b9cbb
2 changed files with 38 additions and 20 deletions

View File

@ -106,18 +106,20 @@ public class CalendarDateTypeDescriptor extends AbstractTypeDescriptor<Calendar>
} }
public <X> Calendar wrap(X value, WrapperOptions options) { public <X> Calendar wrap(X value, WrapperOptions options) {
Calendar cal = null;
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if ( Calendar.class.isInstance( value ) ) { if ( Calendar.class.isInstance( value ) ) {
return (Calendar) value; cal = (Calendar) value;
} }
if ( ! Date.class.isInstance( value ) ) { if ( ! Date.class.isInstance( value ) ) {
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }
Calendar cal = new GregorianCalendar(); if ( cal == null ) {
cal = new GregorianCalendar();
if ( Environment.jvmHasTimestampBug() ) { if ( Environment.jvmHasTimestampBug() ) {
final long milliseconds = ( (Date) value ).getTime(); final long milliseconds = ( (Date) value ).getTime();
final long nanoseconds = java.sql.Timestamp.class.isInstance( value ) final long nanoseconds = java.sql.Timestamp.class.isInstance( value )
@ -128,6 +130,14 @@ public class CalendarDateTypeDescriptor extends AbstractTypeDescriptor<Calendar>
else { else {
cal.setTime( (Date) value ); cal.setTime( (Date) value );
} }
}
// Some JDBC drivers (*ahem* Oracle 12c) are incorrect and, even though rs#getDate is used, return the Date
// without the time stripped. For extra safety, ensure here.
cal.set( Calendar.HOUR, 0 );
cal.set( Calendar.MINUTE, 0 );
cal.set( Calendar.SECOND, 0 );
return cal; return cal;
} }
} }

View File

@ -141,25 +141,33 @@ public class JdbcDateTypeDescriptor extends AbstractTypeDescriptor<Date> {
@SuppressWarnings({ "UnnecessaryUnboxing" }) @SuppressWarnings({ "UnnecessaryUnboxing" })
public <X> Date wrap(X value, WrapperOptions options) { public <X> Date wrap(X value, WrapperOptions options) {
java.sql.Date date = null;
if ( value == null ) { if ( value == null ) {
return null; return null;
} }
if ( Date.class.isInstance( value ) ) { if ( java.sql.Date.class.isInstance( value ) ) {
return (Date) value; date = (java.sql.Date) value;
} }
if ( Long.class.isInstance( value ) ) { if ( Long.class.isInstance( value ) ) {
return new java.sql.Date( ( (Long) value ).longValue() ); date = new java.sql.Date( ( (Long) value ).longValue() );
} }
if ( Calendar.class.isInstance( value ) ) { if ( Calendar.class.isInstance( value ) ) {
return new java.sql.Date( ( (Calendar) value ).getTimeInMillis() ); date = new java.sql.Date( ( (Calendar) value ).getTimeInMillis() );
} }
if ( java.util.Date.class.isInstance( value ) ) { if ( Date.class.isInstance( value ) ) {
return new java.sql.Date( ( (java.util.Date) value ).getTime() ); date = new java.sql.Date( ( (java.util.Date) value ).getTime() );
} }
if ( date == null ) {
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }
// Some JDBC drivers (*ahem* Oracle 12c) are incorrect and, even though rs#getDate is used, return the Date
// without the time stripped. For extra safety, ensure here.
return new java.sql.Date(date.getYear(), date.getMonth(), date.getDate());
}
} }