Hibernate incorrectly applies Converters with autoApply=true to subclasses

Description

I've verified that version 4.3.11 does not have this behavior.

I have a class Money that derives from BigDecimal. I have an entity Widget that has a field of type BigDecimal and another of type Money.
If I create a simply MoneyConverter and annotate just the Money field of Widget, the conversion happens just fine. However, if instead, I annotate the MoneyConverter class with @Converter and set autoApply = true, Hibernate attempts to use the MoneyConverter for the field of type BigDecimal. This is an error.

The attached project uses spring-boot. Execute mvn package and then jar -jar target/hibernate-conversion-0.0.1-SNAPSHOT.jar to see the error.
The exception of interest is:
Caused by: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to com.eclecticlogic.hibernate.demo.Money
at com.eclecticlogic.hibernate.demo.CostConverter.convertToDatabaseColumn(CostConverter.java:23)
at org.hibernate.type.descriptor.converter.AttributeConverterMutabilityPlanImpl.deepCopyNotNull(AttributeConverterMutabilityPlanImpl.java:29)

In the project, comment out the @Converter annotation in MoneyConverter.java and then uncomment the @Convert annotation in Widget.java getCost() method. You will see that the project runs just fine.

Environment

Java 8
Hibernate 5.1
Using maven to build/run.

Demo project expects to connect to a postgresql instance. You can configure database name and username/password in the src/main/resources/application.properties file. By default it tries to connect to hibdemo database with username/password of postgres.
Schema is auto-created.

Status

Assignee

Vlad Mihalcea

Reporter

Karthik Abram

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

Affects versions

5.1.0

Priority

Major