|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.mapping.basic; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.Table; |
| 10 | +import org.hibernate.annotations.JdbcTypeCode; |
| 11 | +import org.hibernate.cfg.AvailableSettings; |
| 12 | +import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping; |
| 13 | +import org.hibernate.metamodel.spi.MappingMetamodelImplementor; |
| 14 | +import org.hibernate.persister.entity.EntityPersister; |
| 15 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks; |
| 16 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 17 | +import org.hibernate.testing.orm.junit.Jira; |
| 18 | +import org.hibernate.testing.orm.junit.RequiresDialectFeature; |
| 19 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 22 | +import org.hibernate.testing.orm.junit.Setting; |
| 23 | +import org.hibernate.type.SqlTypes; |
| 24 | +import org.hibernate.type.descriptor.jdbc.ArrayJdbcType; |
| 25 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 26 | +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 32 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | +import static org.hamcrest.Matchers.isA; |
| 35 | + |
| 36 | +// It's vital that EntityWithJson is listed first to reproduce the bug, |
| 37 | +// the bug being, that JsonJavaType was registered in JavaTypeRegistry under e.g. List<Integer>, |
| 38 | +// which would then wrongly be used for EntityWithArray#listInteger as JavaType |
| 39 | +@DomainModel(annotatedClasses = { JsonAndArrayMappingTests.EntityWithJson.class, JsonAndArrayMappingTests.EntityWithArray.class}) |
| 40 | +@SessionFactory |
| 41 | +@ServiceRegistry(settings = @Setting(name = AvailableSettings.JSON_FORMAT_MAPPER, value = "jackson")) |
| 42 | +@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsTypedArrays.class) |
| 43 | +@Jira("https://hibernate.atlassian.net/browse/HHH-17680") |
| 44 | +public class JsonAndArrayMappingTests { |
| 45 | + |
| 46 | + @Test |
| 47 | + public void verifyMappings(SessionFactoryScope scope) { |
| 48 | + final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory() |
| 49 | + .getRuntimeMetamodels() |
| 50 | + .getMappingMetamodel(); |
| 51 | + final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry(); |
| 52 | + final JdbcType jsonType = jdbcTypeRegistry.getDescriptor( SqlTypes.JSON ); |
| 53 | + final EntityPersister jsonEntity = mappingMetamodel.findEntityDescriptor( EntityWithJson.class ); |
| 54 | + |
| 55 | + final BasicAttributeMapping listStringJsonAttribute = (BasicAttributeMapping) jsonEntity.findAttributeMapping( |
| 56 | + "listString" ); |
| 57 | + final BasicAttributeMapping listIntegerJsonAttribute = (BasicAttributeMapping) jsonEntity.findAttributeMapping( |
| 58 | + "listInteger" ); |
| 59 | + |
| 60 | + assertThat( listStringJsonAttribute.getJavaType().getJavaTypeClass(), equalTo( List.class ) ); |
| 61 | + assertThat( listIntegerJsonAttribute.getJavaType().getJavaTypeClass(), equalTo( List.class ) ); |
| 62 | + |
| 63 | + assertThat( listStringJsonAttribute.getJdbcMapping().getJdbcType(), isA( (Class<JdbcType>) jsonType.getClass() ) ); |
| 64 | + assertThat( listIntegerJsonAttribute.getJdbcMapping().getJdbcType(), isA( (Class<JdbcType>) jsonType.getClass() ) ); |
| 65 | + |
| 66 | + final EntityPersister arrayEntity = mappingMetamodel.findEntityDescriptor( EntityWithArray.class ); |
| 67 | + |
| 68 | + final BasicAttributeMapping listStringArrayAttribute = (BasicAttributeMapping) arrayEntity.findAttributeMapping( |
| 69 | + "listString" ); |
| 70 | + final BasicAttributeMapping listIntegerArrayAttribute = (BasicAttributeMapping) arrayEntity.findAttributeMapping( |
| 71 | + "listInteger" ); |
| 72 | + |
| 73 | + assertThat( listStringArrayAttribute.getJavaType().getJavaTypeClass(), equalTo( List.class ) ); |
| 74 | + assertThat( listIntegerArrayAttribute.getJavaType().getJavaTypeClass(), equalTo( List.class ) ); |
| 75 | + |
| 76 | + assertThat( listStringArrayAttribute.getJdbcMapping().getJdbcType(), instanceOf( ArrayJdbcType.class ) ); |
| 77 | + assertThat( listIntegerArrayAttribute.getJdbcMapping().getJdbcType(), instanceOf( ArrayJdbcType.class ) ); |
| 78 | + } |
| 79 | + |
| 80 | + @Entity(name = "EntityWithJson") |
| 81 | + @Table(name = "EntityWithJson") |
| 82 | + public static class EntityWithJson { |
| 83 | + @Id |
| 84 | + private Integer id; |
| 85 | + |
| 86 | + @JdbcTypeCode( SqlTypes.JSON ) |
| 87 | + private List<String> listString; |
| 88 | + @JdbcTypeCode( SqlTypes.JSON ) |
| 89 | + private List<Integer> listInteger; |
| 90 | + |
| 91 | + public EntityWithJson() { |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Entity(name = "EntityWithArray") |
| 96 | + @Table(name = "EntityWithArray") |
| 97 | + public static class EntityWithArray { |
| 98 | + @Id |
| 99 | + private Integer id; |
| 100 | + |
| 101 | + @JdbcTypeCode( SqlTypes.ARRAY ) |
| 102 | + private List<String> listString; |
| 103 | + @JdbcTypeCode( SqlTypes.ARRAY ) |
| 104 | + private List<Integer> listInteger; |
| 105 | + |
| 106 | + public EntityWithArray() { |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments