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

View File

@ -141,25 +141,33 @@ public class JdbcDateTypeDescriptor extends AbstractTypeDescriptor<Date> {
@SuppressWarnings({ "UnnecessaryUnboxing" })
public <X> Date wrap(X value, WrapperOptions options) {
java.sql.Date date = null;
if ( value == null ) {
return null;
}
if ( Date.class.isInstance( value ) ) {
return (Date) value;
if ( java.sql.Date.class.isInstance( value ) ) {
date = (java.sql.Date) 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 ) ) {
return new java.sql.Date( ( (Calendar) value ).getTimeInMillis() );
date = new java.sql.Date( ( (Calendar) value ).getTimeInMillis() );
}
if ( java.util.Date.class.isInstance( value ) ) {
return new java.sql.Date( ( (java.util.Date) value ).getTime() );
if ( Date.class.isInstance( value ) ) {
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());
}
}