diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/ManagedTypeProcessor.java b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/ManagedTypeProcessor.java index 831be5b28700..9a2e3fa34229 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/ManagedTypeProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/ManagedTypeProcessor.java @@ -209,7 +209,6 @@ private static void processEntityMetadata( XmlAnnotationHelper.applyTable( jaxbEntity.getTable(), classDetails, xmlDocumentContext ); XmlAnnotationHelper.applySecondaryTables( jaxbEntity.getSecondaryTables(), classDetails, xmlDocumentContext ); - final JaxbAttributesContainerImpl attributes = jaxbEntity.getAttributes(); if ( attributes != null ) { processIdMappings( @@ -234,6 +233,9 @@ private static void processEntityMetadata( xmlDocumentContext ); } + + XmlAnnotationHelper.applyConverts( jaxbEntity.getConverts(), classDetails, xmlDocumentContext ); + AttributeProcessor.processAttributeOverrides( jaxbEntity.getAttributeOverrides(), classDetails, diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java index 54bb3cb56d32..e3d3ec32d4d7 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java @@ -135,6 +135,7 @@ import static org.hibernate.boot.models.JpaAnnotations.CHECK_CONSTRAINT; import static org.hibernate.boot.models.JpaAnnotations.COLUMN; import static org.hibernate.boot.models.JpaAnnotations.CONVERT; +import static org.hibernate.boot.models.JpaAnnotations.CONVERTS; import static org.hibernate.boot.models.JpaAnnotations.EXCLUDE_DEFAULT_LISTENERS; import static org.hibernate.boot.models.JpaAnnotations.EXCLUDE_SUPERCLASS_LISTENERS; import static org.hibernate.boot.models.JpaAnnotations.INDEX; @@ -804,6 +805,28 @@ public static void applyConvert( transferConvertDetails( jaxbConvert, annotation, null, xmlDocumentContext ); } + public static void applyConverts( + List jaxbConverts, + MutableAnnotationTarget target, + XmlDocumentContext xmlDocumentContext){ + if ( isEmpty( jaxbConverts ) ) { + return; + } + + final ConvertsJpaAnnotation convertsUsage = (ConvertsJpaAnnotation) target.replaceAnnotationUsage( + CONVERT, + CONVERTS, + xmlDocumentContext.getModelBuildingContext() + ); + + final Convert[] convertUsages = new Convert[jaxbConverts.size()]; + convertsUsage.value( convertUsages ); + + for ( int i = 0; i < jaxbConverts.size(); i++ ) { + convertUsages[i] = transformConvert( jaxbConverts.get( i ), null, xmlDocumentContext ); + } + } + public static void applyConverts( List jaxbConverts, String namePrefix, @@ -815,7 +838,7 @@ public static void applyConverts( final ConvertsJpaAnnotation convertsUsage = (ConvertsJpaAnnotation) memberDetails.replaceAnnotationUsage( CONVERT, - JpaAnnotations.CONVERTS, + CONVERTS, xmlDocumentContext.getModelBuildingContext() ); final Convert[] convertUsages = new Convert[jaxbConverts.size()]; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleXmlOverriddenTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleXmlOverriddenTest.java index f2461f5480f8..6b8f27602f26 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleXmlOverriddenTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleXmlOverriddenTest.java @@ -20,6 +20,7 @@ import org.hibernate.type.Type; import org.hibernate.type.descriptor.java.StringJavaType; import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; +import org.hibernate.type.internal.BasicTypeImpl; import org.hibernate.type.internal.ConvertedBasicTypeImpl; import org.junit.jupiter.api.Test; @@ -66,10 +67,27 @@ public void testDefinitionAtAttributeLevel(ServiceRegistryScope scope) { PersistentClass pc = metadata.getEntityBinding( TheEntity.class.getName() ); BasicType type = (BasicType) pc.getProperty( "it" ).getType(); + assertTyping( BasicTypeImpl.class, type ); // Should not be ConvertedBasicTypeImpl in particular assertTyping( StringJavaType.class, type.getJavaTypeDescriptor() ); assertTyping( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ).getClass(), type.getJdbcType() ); } + /** + * A baseline test, with an explicit @Convert annotation at entity level that should be in effect + */ + @Test + public void baselineAtEntityLevel(ServiceRegistryScope scope) { + Metadata metadata = new MetadataSources( scope.getRegistry() ) + .addAnnotatedClass( TheEntity2.class ) + .buildMetadata(); + + PersistentClass pc = metadata.getEntityBinding( TheEntity2.class.getName() ); + Type type = pc.getProperty( "it" ).getType(); + ConvertedBasicTypeImpl adapter = assertTyping( ConvertedBasicTypeImpl.class, type ); + final JpaAttributeConverter converter = (JpaAttributeConverter) adapter.getValueConverter(); + assertTrue( SillyStringConverter.class.isAssignableFrom( converter.getConverterJavaType().getJavaTypeClass() ) ); + } + /** * Test outcome of applying overrides via orm.xml, specifically at the entity level */ @@ -85,6 +103,7 @@ public void testDefinitionAtEntityLevel(ServiceRegistryScope scope) { PersistentClass pc = metadata.getEntityBinding( TheEntity2.class.getName() ); BasicType type = (BasicType) pc.getProperty( "it" ).getType(); + assertTyping( BasicTypeImpl.class, type ); // Should not be ConvertedBasicTypeImpl in particular assertTyping( StringJavaType.class, type.getJavaTypeDescriptor() ); assertTyping( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ).getClass(), type.getJdbcType() ); }