|
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.SqlTypes; |
13 | 16 | import org.hibernate.type.descriptor.JdbcBindingLogging;
|
14 | 17 |
|
15 | 18 | import org.hibernate.testing.orm.junit.JiraKey;
|
|
19 | 22 | import org.hibernate.testing.orm.junit.MessageKeyWatcher;
|
20 | 23 | import org.hibernate.testing.orm.junit.SessionFactory;
|
21 | 24 | import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
| 25 | +import org.hibernate.type.descriptor.java.JavaType; |
| 26 | +import org.hibernate.type.spi.TypeConfiguration; |
22 | 27 | import org.junit.jupiter.api.AfterEach;
|
23 | 28 | import org.junit.jupiter.api.BeforeEach;
|
24 | 29 | import org.junit.jupiter.api.Test;
|
25 | 30 |
|
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertSame; |
26 | 34 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
27 | 35 |
|
28 | 36 | /**
|
@@ -134,6 +142,41 @@ public void hqlTestEnumQualifiedShortHandSyntaxInPredicate(SessionFactoryScope s
|
134 | 142 | );
|
135 | 143 | }
|
136 | 144 |
|
| 145 | + @Test |
| 146 | + @Jira( "https://hibernate.atlassian.net/browse/HHH-19276" ) |
| 147 | + public void testNoEnumMemoryLeak(SessionFactoryScope scope) { |
| 148 | + final List<HairColor> colors = List.of(HairColor.BLACK, HairColor.BROWN); |
| 149 | + final TypeConfiguration typeConfiguration = scope.getSessionFactory().getTypeConfiguration(); |
| 150 | + final JavaType<HairColor> hairColorJavaType = getEnumJavaType( typeConfiguration, HairColor.class ); |
| 151 | + final BasicType<HairColor> hairColorBasicType = getBasicTypeForEnumJavaType( typeConfiguration, HairColor.class ); |
| 152 | + // Basically, multiple runs of this should not result in the creation of additional |
| 153 | + // EnumJavaTypes (or BasicTypes for that matter), as was the case before the fix for HHH-19276 |
| 154 | + for (int counter = 1; counter <= 10; counter++ ) { |
| 155 | + scope.inTransaction( |
| 156 | + (session) -> { |
| 157 | + var result = session.createNativeQuery( |
| 158 | + "SELECT * FROM Person WHERE hairColor in (:colors) FOR UPDATE SKIP LOCKED", |
| 159 | + Person.class |
| 160 | + ) |
| 161 | + .setParameter( "colors", colors ) |
| 162 | + .list(); |
| 163 | + } |
| 164 | + ); |
| 165 | + JavaType<HairColor> enumJavaType = getEnumJavaType( typeConfiguration, HairColor.class ); |
| 166 | + BasicType<HairColor> basicType = getBasicTypeForEnumJavaType( typeConfiguration, HairColor.class ); |
| 167 | + assertSame( hairColorJavaType, enumJavaType ); |
| 168 | + assertSame( hairColorBasicType, basicType ); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private <T> JavaType<T> getEnumJavaType(TypeConfiguration typeConfiguration, Class<T> enumJavaType) { |
| 173 | + return typeConfiguration.getJavaTypeRegistry().resolveDescriptor(enumJavaType); |
| 174 | + } |
| 175 | + |
| 176 | + private <T> BasicType<T> getBasicTypeForEnumJavaType(TypeConfiguration typeConfiguration, Class<T> enumJavaType) { |
| 177 | + return typeConfiguration.getBasicTypeRegistry().resolve(enumJavaType, SqlTypes.TINYINT ); |
| 178 | + } |
| 179 | + |
137 | 180 | @Entity(name = "Person")
|
138 | 181 | public static class Person {
|
139 | 182 |
|
|
0 commit comments