Skip to content

Commit 37c1f88

Browse files
committed
add additional test for findMultiple() with second-level cache
1 parent 9e928b6 commit 37c1f88

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.loading.multiLoad;
6+
7+
import jakarta.persistence.Cacheable;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.Id;
10+
import org.hibernate.LockMode;
11+
import org.hibernate.testing.orm.junit.DomainModel;
12+
import org.hibernate.testing.orm.junit.SessionFactory;
13+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
14+
import org.junit.jupiter.api.Test;
15+
16+
import java.util.List;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
20+
21+
@SessionFactory(generateStatistics = true)
22+
@DomainModel(annotatedClasses = FindMultipleFromCacheTest.Record.class)
23+
public class FindMultipleFromCacheTest {
24+
@Test void test(SessionFactoryScope scope) {
25+
scope.inStatelessTransaction(s-> {
26+
s.insert(new Record(123L,"hello earth"));
27+
s.insert(new Record(456L,"hello mars"));
28+
});
29+
scope.inTransaction(s-> {
30+
List<Record> all = s.findMultiple(Record.class, List.of(456L, 123L, 2L));
31+
Record mars = all.get( 0 );
32+
Record earth = all.get( 1 );
33+
assertEquals( LockMode.READ, s.getCurrentLockMode( mars ) );
34+
assertEquals( LockMode.READ, s.getCurrentLockMode( earth ) );
35+
assertEquals("hello mars", mars.message);
36+
assertEquals("hello earth", earth.message);
37+
assertNull(all.get(2));
38+
});
39+
assertEquals( 0,
40+
scope.getSessionFactory().getStatistics().getSecondLevelCacheHitCount() );
41+
scope.getSessionFactory().getStatistics().clear();
42+
scope.inTransaction(s-> {
43+
List<Record> all = s.findMultiple(Record.class, List.of(123L, 2L, 456L));
44+
Record earth = all.get( 0 );
45+
Record mars = all.get( 2 );
46+
assertEquals( LockMode.NONE, s.getCurrentLockMode( mars ) );
47+
assertEquals( LockMode.NONE, s.getCurrentLockMode( earth ) );
48+
assertEquals("hello earth", earth.message);
49+
assertEquals("hello mars", mars.message);
50+
assertNull(all.get(1));
51+
});
52+
assertEquals( 2,
53+
scope.getSessionFactory().getStatistics().getSecondLevelCacheHitCount() );
54+
}
55+
@Entity @Cacheable
56+
static class Record {
57+
@Id Long id;
58+
String message;
59+
60+
Record(Long id, String message) {
61+
this.id = id;
62+
this.message = message;
63+
}
64+
65+
Record() {
66+
}
67+
}
68+
}

hibernate-core/src/test/java/org/hibernate/orm/test/stateless/GetMultipleFromCacheTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class GetMultipleFromCacheTest {
3131
assertEquals("hello earth",all.get(1).message);
3232
assertNull(all.get(2));
3333
});
34+
assertEquals( 0,
35+
scope.getSessionFactory().getStatistics().getSecondLevelCacheHitCount() );
3436
scope.getSessionFactory().getStatistics().clear();
3537
scope.inStatelessTransaction(s-> {
3638
List<Record> all = s.getMultiple(Record.class, List.of(123L, 2L, 456L));

0 commit comments

Comments
 (0)