Skip to content

Commit 5d348fc

Browse files
shystephbeikov
authored andcommitted
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.
1 parent 439fac7 commit 5d348fc

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,22 @@ public String getFriendlyName() {
203203
public String toString() {
204204
return "ArrayTypeDescriptor";
205205
}
206+
207+
/**
208+
* Check equality. Needed so that ArrayJdbcType in collections correctly match each other.
209+
*
210+
* @param o other object
211+
* @return true if the two array types share the same element type
212+
*/
213+
@Override
214+
public boolean equals(Object o) {
215+
return o != null &&
216+
getClass() == o.getClass() &&
217+
getElementJdbcType().equals( ((ArrayJdbcType) o).getElementJdbcType() );
218+
}
219+
220+
@Override
221+
public int hashCode() {
222+
return getJdbcTypeCode() + getElementJdbcType().hashCode();
223+
}
206224
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.hibernate.orm.test.type.descriptor.jdbc;
2+
3+
import org.hibernate.testing.TestForIssue;
4+
import org.hibernate.type.descriptor.jdbc.ArrayJdbcType;
5+
import org.hibernate.type.descriptor.jdbc.BigIntJdbcType;
6+
import org.hibernate.type.descriptor.jdbc.IntegerJdbcType;
7+
import org.hibernate.type.descriptor.jdbc.JdbcType;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
15+
public class ArrayJdbcTypeTest {
16+
@Test
17+
@TestForIssue(jiraKey = "HHH-17662")
18+
public void testEquality() {
19+
Map<JdbcType, String> typeMap = new HashMap<>();
20+
JdbcType bigInt = new BigIntJdbcType();
21+
typeMap.put(new ArrayJdbcType(bigInt), "bees");
22+
typeMap.put(new ArrayJdbcType(bigInt), "bees");
23+
typeMap.put(new ArrayJdbcType(bigInt), "bees");
24+
typeMap.put(new ArrayJdbcType(new IntegerJdbcType()), "waffles");
25+
assertThat("A map of arrays only contains non duplicate entries", typeMap.size() == 2);
26+
}
27+
}

0 commit comments

Comments
 (0)