Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-18012 Fix array type matching for auto applying AttributeConverter #8246

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public static boolean typesMatch(ResolvedType converterDefinedType, ResolvedType
if ( erasedCheckType.isPrimitive() ) {
erasedCheckType = PrimitiveWrapperHelper.getDescriptorByPrimitiveType( erasedCheckType ).getWrapperClass();
}
else if ( erasedCheckType.isArray() ) {
// converterDefinedType have type parameters if it extends super generic class
// but checkType doesn't have any type parameters
// comparing erased type is enough
// see https://hibernate.atlassian.net/browse/HHH-18012
return converterDefinedType.getErasedType() == erasedCheckType;
}

if ( !converterDefinedType.getErasedType().isAssignableFrom( erasedCheckType ) ) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> implements AttributeConverter<T[], String> {

@Override
public String convertToDatabaseColumn(T[] array) {
return null;
}

@Override
public T[] convertToEntityAttribute(String string) {
return null;
}
}

@Converter(autoApply = true)
static class IntegerArrayConverter extends AbstractArrayConverter<Integer> {

}

static abstract class AbstractListConverter<T> implements AttributeConverter<List<T>, String> {

@Override
public String convertToDatabaseColumn(List<T> array) {
return null;
}

@Override
public List<T> convertToEntityAttribute(String string) {
return null;
}

}

@Converter(autoApply = true)
static class IntegerListConverter extends AbstractListConverter<Integer> {

}

@Entity
static class TestEntity {

@Id
@GeneratedValue
Long id;

Integer[] integerArray;

List<Integer> integerList;
}

}