|
10 | 10 | import jakarta.persistence.GeneratedValue;
|
11 | 11 | import jakarta.persistence.Id;
|
12 | 12 |
|
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.type.BasicType; |
| 15 | +import org.hibernate.type.BasicTypeRegistry; |
| 16 | +import org.hibernate.type.SqlTypes; |
13 | 17 | import org.hibernate.type.descriptor.JdbcBindingLogging;
|
14 | 18 |
|
15 | 19 | import org.hibernate.testing.orm.junit.JiraKey;
|
|
19 | 23 | import org.hibernate.testing.orm.junit.MessageKeyWatcher;
|
20 | 24 | import org.hibernate.testing.orm.junit.SessionFactory;
|
21 | 25 | import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
| 26 | +import org.hibernate.type.descriptor.java.JavaType; |
| 27 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 28 | +import org.hibernate.type.spi.TypeConfiguration; |
22 | 29 | import org.junit.jupiter.api.AfterEach;
|
23 | 30 | import org.junit.jupiter.api.BeforeEach;
|
24 | 31 | import org.junit.jupiter.api.Test;
|
25 | 32 |
|
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
26 | 37 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
27 | 38 |
|
28 | 39 | /**
|
@@ -134,6 +145,55 @@ public void hqlTestEnumQualifiedShortHandSyntaxInPredicate(SessionFactoryScope s
|
134 | 145 | );
|
135 | 146 | }
|
136 | 147 |
|
| 148 | + @Test |
| 149 | + @Jira( "https://hibernate.atlassian.net/browse/HHH-19276" ) |
| 150 | + public void testNoEnumMemoryLeak(SessionFactoryScope scope) { |
| 151 | + final List<HairColor> colors = List.of(HairColor.BLACK, HairColor.BROWN); |
| 152 | + |
| 153 | + final TypeConfiguration typeConfiguration = scope.getSessionFactory().getTypeConfiguration(); |
| 154 | + final TestingBasicTypeRegistry basicTypeRegistry = new TestingBasicTypeRegistry( typeConfiguration.getBasicTypeRegistry() ); |
| 155 | + final Map<JdbcType, Map<JavaType<?>, BasicType<?>>> registryValues = basicTypeRegistry.getRegistryValues(); |
| 156 | + final Map<JavaType<?>, BasicType<?>> enumJavaTypeValues = registryValues.get( typeConfiguration.getJdbcTypeRegistry().findDescriptor( SqlTypes.TINYINT ) ); |
| 157 | + |
| 158 | + checkEnumTypeRegistryValues( enumJavaTypeValues ); |
| 159 | + |
| 160 | + // Basically, multiple runs of this should not result in the creation of additional |
| 161 | + // EnumJavaTypes (or BasicTypes for that matter), as was the case before the fix for HHH-19276 |
| 162 | + for (int counter = 1; counter <= 10; counter++ ) { |
| 163 | + scope.inTransaction( |
| 164 | + (session) -> { |
| 165 | + var result = session.createNativeQuery( |
| 166 | + "SELECT * FROM Person WHERE hairColor in (:colors)", |
| 167 | + Person.class |
| 168 | + ) |
| 169 | + .setParameter( "colors", colors ) |
| 170 | + .list(); |
| 171 | + } |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + checkEnumTypeRegistryValues( enumJavaTypeValues ); |
| 176 | + } |
| 177 | + |
| 178 | + private void checkEnumTypeRegistryValues(Map<JavaType<?>, BasicType<?>> values) { |
| 179 | + assertEquals( 2, values.size() ); |
| 180 | + boolean genderAccountedFor = false; |
| 181 | + boolean hairColorAccountedFor = false; |
| 182 | + for (JavaType<?> type : values.keySet()) { |
| 183 | + genderAccountedFor = genderAccountedFor || type.getTypeName().equals( "org.hibernate.orm.test.mapping.converted.enums.Gender" ); |
| 184 | + hairColorAccountedFor = hairColorAccountedFor || type.getTypeName().equals( "org.hibernate.orm.test.mapping.converted.enums.HairColor" ); |
| 185 | + } |
| 186 | + assertTrue( genderAccountedFor && hairColorAccountedFor ); |
| 187 | + } |
| 188 | + |
| 189 | + private <T> JavaType<T> getEnumJavaType(TypeConfiguration typeConfiguration, Class<T> enumJavaType) { |
| 190 | + return typeConfiguration.getJavaTypeRegistry().resolveDescriptor(enumJavaType); |
| 191 | + } |
| 192 | + |
| 193 | + private <T> BasicType<T> getBasicTypeForEnumJavaType(TypeConfiguration typeConfiguration, Class<T> enumJavaType) { |
| 194 | + return typeConfiguration.getBasicTypeRegistry().resolve(enumJavaType, SqlTypes.TINYINT ); |
| 195 | + } |
| 196 | + |
137 | 197 | @Entity(name = "Person")
|
138 | 198 | public static class Person {
|
139 | 199 |
|
@@ -189,4 +249,15 @@ public void setOriginalHairColor(HairColor originalHairColor) {
|
189 | 249 | this.originalHairColor = originalHairColor;
|
190 | 250 | }
|
191 | 251 | }
|
| 252 | + |
| 253 | + private static final class TestingBasicTypeRegistry extends BasicTypeRegistry { |
| 254 | + private TestingBasicTypeRegistry(BasicTypeRegistry basicTypeRegistry){ |
| 255 | + super(basicTypeRegistry); |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + protected Map<JdbcType, Map<JavaType<?>, BasicType<?>>> getRegistryValues() { |
| 260 | + return super.getRegistryValues(); |
| 261 | + } |
| 262 | + } |
192 | 263 | }
|
0 commit comments