From 5d348fc723781a2318648ebd20276f9211a61dfd Mon Sep 17 00:00:00 2001 From: Stephanie Miller Date: Tue, 23 Jan 2024 12:18:32 -0500 Subject: [PATCH] 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. --- .../type/descriptor/jdbc/ArrayJdbcType.java | 18 +++++++++++++ .../descriptor/jdbc/ArrayJdbcTypeTest.java | 27 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/jdbc/ArrayJdbcTypeTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java index 555447b6ab..f83ebd131e 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java @@ -203,4 +203,22 @@ public class ArrayJdbcType implements JdbcType { public String toString() { 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(); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/jdbc/ArrayJdbcTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/jdbc/ArrayJdbcTypeTest.java new file mode 100644 index 0000000000..22d5a3125e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/jdbc/ArrayJdbcTypeTest.java @@ -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 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); + } +}