|
| 1 | +package org.hibernate.orm.test.caching; |
| 2 | + |
| 3 | +import java.time.Instant; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.hibernate.annotations.Cache; |
| 7 | +import org.hibernate.annotations.CacheConcurrencyStrategy; |
| 8 | +import org.hibernate.cfg.AvailableSettings; |
| 9 | +import org.hibernate.dialect.PostgreSQLDialect; |
| 10 | + |
| 11 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 12 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 13 | +import org.hibernate.testing.orm.junit.Jpa; |
| 14 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 15 | +import org.hibernate.testing.orm.junit.Setting; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.Cacheable; |
| 21 | +import jakarta.persistence.Entity; |
| 22 | +import jakarta.persistence.Id; |
| 23 | + |
| 24 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 27 | + |
| 28 | +@JiraKey("HHH-17997") |
| 29 | +@Jpa( |
| 30 | + annotatedClasses = { |
| 31 | + CachingWithTriggerTest.TestEntity.class |
| 32 | + }, |
| 33 | + integrationSettings = { |
| 34 | + @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"), |
| 35 | + @Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "false"), |
| 36 | + } |
| 37 | +) |
| 38 | +@RequiresDialect(value = PostgreSQLDialect.class, comment = "To write a trigger only once") |
| 39 | +public class CachingWithTriggerTest { |
| 40 | + |
| 41 | + private static final String TRIGGER = "begin NEW.lastUpdatedAt = current_timestamp; return NEW; end;"; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void prepare(EntityManagerFactoryScope scope) { |
| 45 | + scope.inTransaction( |
| 46 | + s -> { |
| 47 | + s.createNativeQuery( "create function update_ts_func() returns trigger language plpgsql as $$ " + TRIGGER + " $$" ) |
| 48 | + .executeUpdate(); |
| 49 | + s.createNativeQuery( "create trigger update_ts before insert on TestEntity for each row execute procedure update_ts_func()" ) |
| 50 | + .executeUpdate(); |
| 51 | + } |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @AfterEach |
| 56 | + public void cleanup(EntityManagerFactoryScope scope) { |
| 57 | + scope.inTransaction( |
| 58 | + s -> { |
| 59 | + s.createNativeQuery( "drop trigger if exists update_ts on TestEntity" ) |
| 60 | + .executeUpdate(); |
| 61 | + s.createNativeQuery( "drop function if exists update_ts_func()" ) |
| 62 | + .executeUpdate(); |
| 63 | + s.createQuery( "delete from TestEntity" ).executeUpdate(); |
| 64 | + } |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testPersistThenRefresh(EntityManagerFactoryScope scope) { |
| 70 | + final long testEntityId = 1L; |
| 71 | + |
| 72 | + scope.inTransaction( |
| 73 | + entityManager -> { |
| 74 | + TestEntity entity = new TestEntity( testEntityId, "test" ); |
| 75 | + entityManager.persist( entity ); |
| 76 | + entityManager.flush(); |
| 77 | + // No reload happens |
| 78 | + assertThat( entity.lastUpdatedAt ).isNull(); |
| 79 | + } |
| 80 | + ); |
| 81 | + scope.inTransaction( |
| 82 | + entityManager -> { |
| 83 | + TestEntity entity = entityManager.find( TestEntity.class, testEntityId ); |
| 84 | + entityManager.refresh( entity ); |
| 85 | + // On refresh, we want the actual data from the database |
| 86 | + assertThat( entity.lastUpdatedAt ).isNotNull(); |
| 87 | + } |
| 88 | + ); |
| 89 | + scope.inTransaction( |
| 90 | + entityManager -> { |
| 91 | + TestEntity entity = entityManager.find( TestEntity.class, testEntityId ); |
| 92 | + // Ensure that we don't get stale data |
| 93 | + assertThat( entity.lastUpdatedAt ).isNotNull(); |
| 94 | + } |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testPersistThenRefreshInTransaction(EntityManagerFactoryScope scope) { |
| 100 | + final long testEntityId = 1L; |
| 101 | + |
| 102 | + scope.inTransaction( |
| 103 | + entityManager -> { |
| 104 | + TestEntity entity = new TestEntity( testEntityId, "test" ); |
| 105 | + entityManager.persist( entity ); |
| 106 | + entityManager.flush(); |
| 107 | + entityManager.refresh( entity ); |
| 108 | + // On refresh, we want the actual data from the database |
| 109 | + assertThat( entity.lastUpdatedAt ).isNotNull(); |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + scope.inTransaction( |
| 114 | + entityManager -> { |
| 115 | + TestEntity entity = entityManager.find( TestEntity.class, testEntityId ); |
| 116 | + // Ensure that we don't get stale data |
| 117 | + assertThat( entity.lastUpdatedAt ).isNotNull(); |
| 118 | + } |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testPersistThenRefreshClearAndQueryInTransaction(EntityManagerFactoryScope scope) { |
| 124 | + final long testEntityId = 1L; |
| 125 | + |
| 126 | + scope.inTransaction( |
| 127 | + entityManager -> { |
| 128 | + TestEntity entity = new TestEntity( testEntityId, "test" ); |
| 129 | + entityManager.persist( entity ); |
| 130 | + entityManager.flush(); |
| 131 | + entityManager.refresh( entity ); |
| 132 | + // On refresh, we want the actual data from the database |
| 133 | + assertThat( entity.lastUpdatedAt ).isNotNull(); |
| 134 | + entityManager.clear(); |
| 135 | + |
| 136 | + entity = entityManager.find( TestEntity.class, testEntityId ); |
| 137 | + // Ensure that we don't get stale data |
| 138 | + assertThat( entity.lastUpdatedAt ).isNotNull(); |
| 139 | + } |
| 140 | + ); |
| 141 | + |
| 142 | + scope.inTransaction( |
| 143 | + entityManager -> { |
| 144 | + TestEntity entity = entityManager.find( TestEntity.class, testEntityId ); |
| 145 | + // Ensure that we don't get stale data |
| 146 | + assertThat( entity.lastUpdatedAt ).isNotNull(); |
| 147 | + } |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + @Entity(name = "TestEntity") |
| 152 | + @Cacheable |
| 153 | + @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) |
| 154 | + public static class TestEntity { |
| 155 | + @Id |
| 156 | + private Long id; |
| 157 | + |
| 158 | + private String name; |
| 159 | + |
| 160 | + private Instant lastUpdatedAt; |
| 161 | + |
| 162 | + public TestEntity() { |
| 163 | + } |
| 164 | + |
| 165 | + public TestEntity(Long id, String name) { |
| 166 | + this.id = id; |
| 167 | + this.name = name; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments