From f27b3a956db4f9856bf7a23397f44ecc56bcb283 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 30 Sep 2022 11:58:51 +0200 Subject: [PATCH] HHH-15552 Embeddable type cannot be cast to org.hibernate.usertype.CompositeUserType if referred to from a mapped superclass with generic parameter --- .../hibernate/cfg/ClassPropertyHolder.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/ClassPropertyHolder.java b/hibernate-core/src/main/java/org/hibernate/cfg/ClassPropertyHolder.java index 5829628efa..aed22e582b 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/ClassPropertyHolder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/ClassPropertyHolder.java @@ -259,11 +259,15 @@ public class ClassPropertyHolder extends AbstractPropertyHolder { final Value originalValue = prop.getValue(); if ( originalValue instanceof SimpleValue ) { // Avoid copying when the property doesn't depend on a type variable - if ( inferredData.getTypeName().equals( ( (SimpleValue) originalValue ).getTypeName() ) ) { + if ( inferredData.getTypeName().equals( getTypeName( originalValue ) ) ) { superclass.addDeclaredProperty( prop ); return; } } + if ( originalValue instanceof Component ) { + superclass.addDeclaredProperty( prop ); + return; + } // If the property depends on a type variable, we have to copy it and the Value final Property actualProperty = prop.copy(); actualProperty.setReturnedClassName( inferredData.getTypeName() ); @@ -294,6 +298,19 @@ public class ClassPropertyHolder extends AbstractPropertyHolder { else { setTypeName( value, inferredData.getTypeName() ); } +// if ( value instanceof Component ) { +// Component component = ( (Component) value ); +// Iterator propertyIterator = component.getPropertyIterator(); +// while ( propertyIterator.hasNext() ) { +// Property property = propertyIterator.next(); +// try { +// property.getGetter( component.getComponentClass() ); +// } +// catch (PropertyNotFoundException e) { +// propertyIterator.remove(); +// } +// } +// } actualProperty.setValue( value ); superclass.addDeclaredProperty( actualProperty ); break; @@ -302,6 +319,18 @@ public class ClassPropertyHolder extends AbstractPropertyHolder { } } + private String getTypeName(Value value) { + if ( value instanceof Component ) { + final Component component = (Component) value; + final String typeName = component.getTypeName(); + if ( typeName != null ) { + return typeName; + } + return component.getComponentClassName(); + } + return ( (SimpleValue) value ).getTypeName(); + } + private void setTypeName(Value value, String typeName) { if ( value instanceof ToOne ) { final ToOne toOne = (ToOne) value; @@ -311,7 +340,9 @@ public class ClassPropertyHolder extends AbstractPropertyHolder { else if ( value instanceof Component ) { final Component component = (Component) value; component.setComponentClassName( typeName ); - component.setTypeName( typeName ); + if ( component.getTypeName() != null ) { + component.setTypeName( typeName ); + } } else if ( value instanceof SimpleValue ) { ( (SimpleValue) value ).setTypeName( typeName );