|
| 1 | +package org.hibernate.orm.test.caching; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.hibernate.annotations.Cache; |
| 6 | +import org.hibernate.annotations.CacheConcurrencyStrategy; |
| 7 | +import org.hibernate.cfg.AvailableSettings; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 10 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 11 | +import org.hibernate.testing.orm.junit.Jpa; |
| 12 | +import org.hibernate.testing.orm.junit.Setting; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.Cacheable; |
| 17 | +import jakarta.persistence.Entity; |
| 18 | +import jakarta.persistence.Id; |
| 19 | + |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | +import static org.junit.Assert.assertFalse; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +@JiraKey("HHH-17997") |
| 25 | +@Jpa( |
| 26 | + annotatedClasses = { |
| 27 | + ChacheReadOnlyStartegyTest.TestEntity.class |
| 28 | + }, |
| 29 | + integrationSettings = { |
| 30 | + @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"), |
| 31 | + @Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "false"), |
| 32 | + } |
| 33 | +) |
| 34 | +public class ChacheReadOnlyStartegyTest { |
| 35 | + |
| 36 | + @AfterEach |
| 37 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 38 | + scope.inTransaction( |
| 39 | + entityManager -> |
| 40 | + entityManager.createQuery( "delete TestEntity" ).executeUpdate() |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testPersistThenClearAndQuery(EntityManagerFactoryScope scope) { |
| 46 | + final long testEntityId = 1l; |
| 47 | + |
| 48 | + scope.inTransaction( |
| 49 | + entityManager -> { |
| 50 | + TestEntity entity = new TestEntity( testEntityId, "test" ); |
| 51 | + entityManager.persist( entity ); |
| 52 | + entityManager.flush(); |
| 53 | + entityManager.clear(); |
| 54 | + List<TestEntity> results = entityManager.createQuery( |
| 55 | + "select t from TestEntity t where t.id = :id", |
| 56 | + TestEntity.class |
| 57 | + ).setParameter( "id", testEntityId ).getResultList(); |
| 58 | + |
| 59 | + assertThat( results.size() ).isEqualTo( 1 ); |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + scope.inTransaction( |
| 64 | + entityManager -> { |
| 65 | + assertTrue( scope.getEntityManagerFactory() |
| 66 | + .getCache() |
| 67 | + .contains( TestEntity.class, testEntityId ) ); |
| 68 | + } |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testPersistThenClearAndQueryWithRollback(EntityManagerFactoryScope scope) { |
| 74 | + final long testEntityId = 1l; |
| 75 | + |
| 76 | + scope.inEntityManager( |
| 77 | + entityManager -> { |
| 78 | + entityManager.getTransaction().begin(); |
| 79 | + try { |
| 80 | + TestEntity entity = new TestEntity( testEntityId, "test" ); |
| 81 | + entityManager.persist( entity ); |
| 82 | + entityManager.flush(); |
| 83 | + entityManager.clear(); |
| 84 | + List<TestEntity> results = entityManager.createQuery( |
| 85 | + "select t from TestEntity t where t.id = :id", |
| 86 | + TestEntity.class |
| 87 | + ).setParameter( "id", testEntityId ).getResultList(); |
| 88 | + |
| 89 | + assertThat( results.size() ).isEqualTo( 1 ); |
| 90 | + } |
| 91 | + finally { |
| 92 | + entityManager.getTransaction().rollback(); |
| 93 | + } |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + scope.inTransaction( |
| 98 | + entityManager -> { |
| 99 | + assertFalse( scope.getEntityManagerFactory() |
| 100 | + .getCache() |
| 101 | + .contains( TestEntity.class, testEntityId ) ); |
| 102 | + List<TestEntity> results = entityManager.createQuery( |
| 103 | + "select t from TestEntity t where t.id = :id", |
| 104 | + TestEntity.class |
| 105 | + ).setParameter( "id", testEntityId ).getResultList(); |
| 106 | + |
| 107 | + assertThat( results.size() ).isEqualTo( 0 ); |
| 108 | + } |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + @Entity(name = "TestEntity") |
| 113 | + @Cacheable |
| 114 | + @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) |
| 115 | + public static class TestEntity { |
| 116 | + @Id |
| 117 | + private Long id; |
| 118 | + |
| 119 | + private String name; |
| 120 | + |
| 121 | + public TestEntity() { |
| 122 | + } |
| 123 | + |
| 124 | + public TestEntity(Long id, String name) { |
| 125 | + this.id = id; |
| 126 | + this.name = name; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments