We're updating the issue view to help you get more done. 

Strange date type confusion in JdbcDateTypeDescriptor

Description

When migrating from 5.2.17 to 5.3.0 I started seeing PersistenceException caused by ClassCastException

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 javax.persistence.PersistenceException: Error attempting to apply AttributeConverter at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.doConversion(AttributeConverterSqlTypeDescriptorAdapter.java:148) at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.extract(AttributeConverterSqlTypeDescriptorAdapter.java:121) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247) at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:333) at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2964) a... at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) Caused by: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.sql.Date at com.acme.AcmeConverter.convertToEntityAttribute(LocalDateConverter.java:1) at org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl.toDomainValue(JpaAttributeConverterImpl.java:45) at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.doConversion(AttributeConverterSqlTypeDescriptorAdapter.java:140) ... 134 more

Which prompted me to look at JdbcDateTypeDescriptor. To the untrained eye several things look strange:

  1. The type parameter is java.util.Date and not java.sql.Date. I get the impression that DateTypeDescriptor is intended for java.util.Date and JdbcDateTypeDescriptor is intended for java.sql.Date

  2. org.hibernate.type.descriptor.java.JdbcDateTypeDescriptor.wrap(X, WrapperOptions) returns a java.util.Date if a java.util.Date is passed in, otherwise a java.sql.Date is returned. That means when a java.sql.Timestamp is passed in the very same instance is returned. This explains the ClassCastException in my case.

Similar points apply to JdbcTimestampTypeDescriptor and JdbcTimeTypeDescriptor.

I can provide a PR if you agree with my assessment.

Environment

None

Status

Assignee

Chris Cranford

Reporter

Philippe Marschall

Components

Fix versions

Affects versions

5.3.0.Final

Priority

Major