diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java index 9afa44c11130..f1e247cb4f13 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java @@ -101,6 +101,7 @@ import org.hibernate.type.CustomType; import org.hibernate.type.ForeignKeyDirection; import org.hibernate.type.StandardBasicTypes; +import org.hibernate.type.internal.BasicTypeImpl; import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.usertype.CompositeUserType; import org.hibernate.usertype.ParameterizedType; @@ -1726,11 +1727,14 @@ private void resolveLob(final SingularAttributeSourceBasic attributeSource, Simp // Resolves whether the property is LOB based on the type attribute on the attribute property source. // Essentially this expects the type to map to a CLOB/NCLOB/BLOB sql type internally and compares. if ( !value.isLob() && value.getTypeName() != null ) { - final BasicType basicType = attributeSource.getBuildingContext() - .getMetadataCollector() - .getTypeConfiguration() - .getBasicTypeRegistry() - .getRegisteredType( value.getTypeName() ); + final String typeName = value.getTypeName(); + final MetadataBuildingContext context = attributeSource.getBuildingContext(); + final BasicType basicType = + typeName.startsWith( BasicTypeImpl.EXTERNALIZED_PREFIX ) + ? context.getBootstrapContext().resolveAdHocBasicType( typeName ) + : context.getMetadataCollector().getTypeConfiguration() + .getBasicTypeRegistry().getRegisteredType( typeName ); + if ( basicType instanceof AbstractSingleColumnStandardBasicType ) { if ( isLob( basicType.getJdbcType().getDdlTypeCode(), null ) ) { value.makeLob(); diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/lob/LargeObjectMappingTest.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/lob/LargeObjectMappingTest.java new file mode 100644 index 000000000000..c8fc221f37f5 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/lob/LargeObjectMappingTest.java @@ -0,0 +1,91 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.envers.integration.lob; + +import java.sql.Types; +import java.util.Arrays; +import java.util.Objects; + +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.envers.Audited; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Property; +import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; + +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; + +import static org.junit.Assert.assertTrue; + +/** + * @author Armin Krezović (armin.krezovic at ziragroup dot com) + */ +@JiraKey(value = "HHH-16253") +public class LargeObjectMappingTest extends BaseEnversJPAFunctionalTestCase { + + @Entity + @Audited + public static class LargeObjectTestEntity { + @Id + @GeneratedValue + private Integer id; + + @JdbcTypeCode(Types.CLOB) + private String clob; + + @JdbcTypeCode(Types.BLOB) + private byte[] blob; + + public LargeObjectTestEntity() { + } + + public LargeObjectTestEntity(Integer id, String clob, byte[] blob) { + this.id = id; + this.clob = clob; + this.blob = blob; + } + + @Override + public final boolean equals(Object o) { + if ( this == o ) { + return true; + } + + if ( !( o instanceof LargeObjectTestEntity that ) ) { + return false; + } + + return Objects.equals( id, that.id ) && Objects.equals( + clob, + that.clob + ) && Arrays.equals( blob, that.blob ); + } + + @Override + public int hashCode() { + return Objects.hash( id, clob, Arrays.hashCode( blob ) ); + } + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { LargeObjectTestEntity.class }; + } + + @Test + public void testLobTypeMapping() { + PersistentClass entityBinding = metadata().getEntityBinding( LargeObjectTestEntity.class.getName() + "_AUD" ); + + Property blobProperty = entityBinding.getProperty( "blob" ); + Property clobProperty = entityBinding.getProperty( "clob" ); + + assertTrue( blobProperty.isLob() ); + assertTrue( clobProperty.isLob() ); + } +}