|
| 1 | +package org.hibernate.orm.test.annotations; |
| 2 | + |
| 3 | +import jakarta.persistence.AttributeConverter; |
| 4 | +import jakarta.persistence.Converter; |
| 5 | +import jakarta.persistence.Entity; |
| 6 | +import jakarta.persistence.GeneratedValue; |
| 7 | +import jakarta.persistence.Id; |
| 8 | +import jakarta.persistence.metamodel.EntityType; |
| 9 | +import jakarta.persistence.metamodel.SingularAttribute; |
| 10 | +import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; |
| 11 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 12 | +import org.hibernate.type.BasicType; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Yanming Zhou |
| 21 | + */ |
| 22 | +@JiraKey("HHH-18012") |
| 23 | +public class GenericConverterAutoApplyTest extends BaseEntityManagerFunctionalTestCase { |
| 24 | + |
| 25 | + @Override |
| 26 | + protected Class<?>[] getAnnotatedClasses() { |
| 27 | + return new Class[]{IntegerArrayConverter.class, IntegerListConverter.class, TestEntity.class}; |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void genericArrayIsAutoApplied() { |
| 32 | + assertAttributeIsMappingToString("integerArray"); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void genericListIsAutoApplied() { |
| 37 | + assertAttributeIsMappingToString("integerList"); |
| 38 | + } |
| 39 | + |
| 40 | + private void assertAttributeIsMappingToString(String name) { |
| 41 | + EntityType<?> entityType = getOrCreateEntityManager().getMetamodel().entity(TestEntity.class); |
| 42 | + assertThat(entityType.getAttribute(name)).isInstanceOfSatisfying(SingularAttribute.class, |
| 43 | + sa -> assertThat(sa.getType()).isInstanceOfSatisfying(BasicType.class, |
| 44 | + bt -> assertThat(bt.getJdbcJavaType().getJavaType()).isEqualTo(String.class) |
| 45 | + )); |
| 46 | + } |
| 47 | + |
| 48 | + static abstract class AbstractArrayConverter<T> implements AttributeConverter<T[], String> { |
| 49 | + |
| 50 | + @Override |
| 51 | + public String convertToDatabaseColumn(T[] array) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public T[] convertToEntityAttribute(String string) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Converter(autoApply = true) |
| 62 | + static class IntegerArrayConverter extends AbstractArrayConverter<Integer> { |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + static abstract class AbstractListConverter<T> implements AttributeConverter<List<T>, String> { |
| 67 | + |
| 68 | + @Override |
| 69 | + public String convertToDatabaseColumn(List<T> array) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public List<T> convertToEntityAttribute(String string) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @Converter(autoApply = true) |
| 81 | + static class IntegerListConverter extends AbstractListConverter<Integer> { |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + @Entity |
| 86 | + static class TestEntity { |
| 87 | + |
| 88 | + @Id |
| 89 | + @GeneratedValue |
| 90 | + Long id; |
| 91 | + |
| 92 | + Integer[] integerArray; |
| 93 | + |
| 94 | + List<Integer> integerList; |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments