Skip to content

Commit 21e7dfc

Browse files
committed
HHH-19276 - Add test for issue
Signed-off-by: Jan Schatteman <[email protected]>
1 parent a2779fa commit 21e7dfc

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/enums/OrdinalEnumTypeTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import jakarta.persistence.GeneratedValue;
1111
import jakarta.persistence.Id;
1212

13+
import org.hibernate.testing.orm.junit.Jira;
14+
import org.hibernate.type.BasicType;
15+
import org.hibernate.type.SqlTypes;
1316
import org.hibernate.type.descriptor.JdbcBindingLogging;
1417

1518
import org.hibernate.testing.orm.junit.JiraKey;
@@ -19,10 +22,15 @@
1922
import org.hibernate.testing.orm.junit.MessageKeyWatcher;
2023
import org.hibernate.testing.orm.junit.SessionFactory;
2124
import org.hibernate.testing.orm.junit.SessionFactoryScope;
25+
import org.hibernate.type.descriptor.java.JavaType;
26+
import org.hibernate.type.spi.TypeConfiguration;
2227
import org.junit.jupiter.api.AfterEach;
2328
import org.junit.jupiter.api.BeforeEach;
2429
import org.junit.jupiter.api.Test;
2530

31+
import java.util.List;
32+
33+
import static org.junit.jupiter.api.Assertions.assertSame;
2634
import static org.junit.jupiter.api.Assertions.assertTrue;
2735

2836
/**
@@ -134,6 +142,41 @@ public void hqlTestEnumQualifiedShortHandSyntaxInPredicate(SessionFactoryScope s
134142
);
135143
}
136144

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+
137180
@Entity(name = "Person")
138181
public static class Person {
139182

0 commit comments

Comments
 (0)