HHH-17662 Equals for ArrayJdbcType

JdbcTypes are put into a map and deduplicated there. Without an equals the ArrayJdbcType leaks because each resolution is created new.
This commit is contained in:
Stephanie Miller 2024-01-23 12:18:32 -05:00 committed by Christian Beikov
parent 439fac712b
commit 5d348fc723
2 changed files with 45 additions and 0 deletions

View File

@ -203,4 +203,22 @@ public class ArrayJdbcType implements JdbcType {
public String toString() { public String toString() {
return "ArrayTypeDescriptor"; return "ArrayTypeDescriptor";
} }
/**
* Check equality. Needed so that ArrayJdbcType in collections correctly match each other.
*
* @param o other object
* @return true if the two array types share the same element type
*/
@Override
public boolean equals(Object o) {
return o != null &&
getClass() == o.getClass() &&
getElementJdbcType().equals( ((ArrayJdbcType) o).getElementJdbcType() );
}
@Override
public int hashCode() {
return getJdbcTypeCode() + getElementJdbcType().hashCode();
}
} }

View File

@ -0,0 +1,27 @@
package org.hibernate.orm.test.type.descriptor.jdbc;
import org.hibernate.testing.TestForIssue;
import org.hibernate.type.descriptor.jdbc.ArrayJdbcType;
import org.hibernate.type.descriptor.jdbc.BigIntJdbcType;
import org.hibernate.type.descriptor.jdbc.IntegerJdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
public class ArrayJdbcTypeTest {
@Test
@TestForIssue(jiraKey = "HHH-17662")
public void testEquality() {
Map<JdbcType, String> typeMap = new HashMap<>();
JdbcType bigInt = new BigIntJdbcType();
typeMap.put(new ArrayJdbcType(bigInt), "bees");
typeMap.put(new ArrayJdbcType(bigInt), "bees");
typeMap.put(new ArrayJdbcType(bigInt), "bees");
typeMap.put(new ArrayJdbcType(new IntegerJdbcType()), "waffles");
assertThat("A map of arrays only contains non duplicate entries", typeMap.size() == 2);
}
}