|
| 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 | +} |
0 commit comments