Skip to content

Commit 26bcbb2

Browse files
committed
HHH-18323 Add test for issue
1 parent 52c80af commit 26bcbb2

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.cache;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.GeneratedValue;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.MappedSuperclass;
11+
import jakarta.persistence.NoResultException;
12+
import jakarta.persistence.OneToOne;
13+
import jakarta.persistence.Version;
14+
import org.hibernate.annotations.Cache;
15+
import org.hibernate.annotations.CacheConcurrencyStrategy;
16+
import org.hibernate.cfg.AvailableSettings;
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.JiraKey;
19+
import org.hibernate.testing.orm.junit.ServiceRegistry;
20+
import org.hibernate.testing.orm.junit.SessionFactory;
21+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
22+
import org.hibernate.testing.orm.junit.Setting;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.sql.Statement;
27+
28+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
29+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
30+
31+
@DomainModel(
32+
annotatedClasses = {
33+
FullLayoutQueryCacheTest.FirstEntity.class,
34+
FullLayoutQueryCacheTest.SecondEntity.class,
35+
}
36+
)
37+
@ServiceRegistry(
38+
settings = {
39+
@Setting(
40+
name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"
41+
),
42+
@Setting(
43+
name = AvailableSettings.USE_QUERY_CACHE, value = "true"
44+
),
45+
@Setting(
46+
name = AvailableSettings.QUERY_CACHE_LAYOUT, value = "FULL"
47+
)
48+
}
49+
)
50+
@SessionFactory
51+
@JiraKey("HHH-18323")
52+
public class FullLayoutQueryCacheTest {
53+
54+
private static final String FIRST_ENTITY_NAME = "FirstEntity";
55+
56+
@BeforeEach
57+
public void setup(SessionFactoryScope scope) {
58+
scope.inTransaction(
59+
session -> {
60+
SecondEntity secondEntity = new SecondEntity( "second" );
61+
session.persist( new FirstEntity( FIRST_ENTITY_NAME, secondEntity ) );
62+
session.persist( secondEntity );
63+
}
64+
);
65+
}
66+
67+
@Test
68+
public void testQueryCache(SessionFactoryScope scope) {
69+
scope.inSession(
70+
session -> {
71+
session.createQuery(
72+
"select f from FirstEntity f where f.name = :name", FirstEntity.class )
73+
.setParameter( "name", FIRST_ENTITY_NAME )
74+
.setCacheable( true )
75+
.getSingleResult();
76+
}
77+
);
78+
79+
deleteEntitiesSilently( scope );
80+
81+
scope.inSession(
82+
session -> {
83+
FirstEntity firstEntity = session.createQuery(
84+
"select f from FirstEntity f where f.name = :name", FirstEntity.class )
85+
.setParameter( "name", FIRST_ENTITY_NAME )
86+
.setCacheable( true )
87+
.getSingleResult();
88+
assertThat( firstEntity ).isNotNull();
89+
}
90+
);
91+
92+
clearCache( scope );
93+
94+
scope.inSession( session -> {
95+
assertThatThrownBy( () ->
96+
session.createQuery(
97+
"select f from FirstEntity f where f.name = :name", FirstEntity.class )
98+
.setParameter( "name", FIRST_ENTITY_NAME )
99+
.setCacheable( true )
100+
.getSingleResult()
101+
).isInstanceOf( NoResultException.class );
102+
103+
} );
104+
}
105+
106+
private static void clearCache(SessionFactoryScope scope) {
107+
scope.getSessionFactory().getCache().evictAll();
108+
scope.getSessionFactory().getCache().evictQueryRegions();
109+
}
110+
111+
private void deleteEntitiesSilently(SessionFactoryScope scope) {
112+
scope.inSession(
113+
session ->
114+
session.doWork(
115+
connection -> {
116+
Statement stmt = null;
117+
try {
118+
stmt = connection.createStatement();
119+
stmt.executeUpdate( "DELETE FROM SecondEntity" );
120+
stmt.executeUpdate( "DELETE FROM FirstEntity" );
121+
}
122+
finally {
123+
if ( stmt != null ) {
124+
stmt.close();
125+
}
126+
}
127+
}
128+
)
129+
);
130+
}
131+
132+
@MappedSuperclass
133+
public static abstract class BaseEntity {
134+
135+
@Id
136+
@GeneratedValue
137+
protected Long id;
138+
139+
@Version
140+
protected int version;
141+
142+
protected String name;
143+
144+
public BaseEntity() {
145+
}
146+
147+
public BaseEntity(String name) {
148+
this.name = name;
149+
}
150+
}
151+
152+
@Entity(name = "FirstEntity")
153+
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
154+
public static class FirstEntity extends BaseEntity {
155+
156+
@OneToOne(mappedBy = "firstEntity")
157+
private SecondEntity secondEntity;
158+
159+
public FirstEntity() {
160+
}
161+
162+
public FirstEntity(String name, SecondEntity secondEntity) {
163+
super( name );
164+
this.secondEntity = secondEntity;
165+
secondEntity.firstEntity = this;
166+
}
167+
}
168+
169+
@Entity(name = "SecondEntity")
170+
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
171+
public static class SecondEntity extends BaseEntity {
172+
173+
@OneToOne
174+
private FirstEntity firstEntity;
175+
176+
public SecondEntity() {
177+
}
178+
179+
public SecondEntity(String baseName) {
180+
super( baseName );
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)