From 9905a3085231f7bb18063e8f2a4f886231d599f4 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Thu, 25 Apr 2024 12:43:17 +0800 Subject: [PATCH] HHH-18012 Add test for issue --- .../GenericConverterAutoApplyTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/annotations/GenericConverterAutoApplyTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/GenericConverterAutoApplyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/GenericConverterAutoApplyTest.java new file mode 100644 index 0000000000..76ad4277f3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/GenericConverterAutoApplyTest.java @@ -0,0 +1,97 @@ +package org.hibernate.orm.test.annotations; + +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Converter; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.metamodel.EntityType; +import jakarta.persistence.metamodel.SingularAttribute; +import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.type.BasicType; +import org.junit.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Yanming Zhou + */ +@JiraKey("HHH-18012") +public class GenericConverterAutoApplyTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{IntegerArrayConverter.class, IntegerListConverter.class, TestEntity.class}; + } + + @Test + public void genericArrayIsAutoApplied() { + assertAttributeIsMappingToString("integerArray"); + } + + @Test + public void genericListIsAutoApplied() { + assertAttributeIsMappingToString("integerList"); + } + + private void assertAttributeIsMappingToString(String name) { + EntityType entityType = getOrCreateEntityManager().getMetamodel().entity(TestEntity.class); + assertThat(entityType.getAttribute(name)).isInstanceOfSatisfying(SingularAttribute.class, + sa -> assertThat(sa.getType()).isInstanceOfSatisfying(BasicType.class, + bt -> assertThat(bt.getJdbcJavaType().getJavaType()).isEqualTo(String.class) + )); + } + + static abstract class AbstractArrayConverter implements AttributeConverter { + + @Override + public String convertToDatabaseColumn(T[] array) { + return null; + } + + @Override + public T[] convertToEntityAttribute(String string) { + return null; + } + } + + @Converter(autoApply = true) + static class IntegerArrayConverter extends AbstractArrayConverter { + + } + + static abstract class AbstractListConverter implements AttributeConverter, String> { + + @Override + public String convertToDatabaseColumn(List array) { + return null; + } + + @Override + public List convertToEntityAttribute(String string) { + return null; + } + + } + + @Converter(autoApply = true) + static class IntegerListConverter extends AbstractListConverter { + + } + + @Entity + static class TestEntity { + + @Id + @GeneratedValue + Long id; + + Integer[] integerArray; + + List integerList; + } + +}