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,28 +106,38 @@ 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 ) {
if ( Environment.jvmHasTimestampBug() ) { cal = new GregorianCalendar();
final long milliseconds = ( (Date) value ).getTime(); if ( Environment.jvmHasTimestampBug() ) {
final long nanoseconds = java.sql.Timestamp.class.isInstance( value ) final long milliseconds = ( (Date) value ).getTime();
? ( (java.sql.Timestamp) value ).getNanos() final long nanoseconds = java.sql.Timestamp.class.isInstance( value )
: 0; ? ( (java.sql.Timestamp) value ).getNanos()
cal.setTime( new Date( milliseconds + nanoseconds / 1000000 ) ); : 0;
} cal.setTime( new Date( milliseconds + nanoseconds / 1000000 ) );
else { }
cal.setTime( (Date) value ); else {
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() );
} }
throw unknownWrap( value.getClass() ); if ( date == null ) {
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());
} }
} }