Uploaded image for project: 'Hibernate ORM'
  1. HHH-12586

Strange date type confusion in JdbcDateTypeDescriptor

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.3.0.Final
    • Fix Version/s: 5.3.1
    • Component/s: hibernate-core
    • Labels:
      None
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true
    • Sprint:

      Description

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

      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.

        Attachments

          Issue links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: