Skip to content

Commit e5307a5

Browse files
kkueglerbeikov
authored andcommitted
HHH-18689 Test FULL query cache sometimes incomplete
1 parent 53be396 commit e5307a5

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.cache;
8+
9+
import org.hibernate.CacheMode;
10+
import org.hibernate.Session;
11+
import org.hibernate.annotations.Cache;
12+
import org.hibernate.annotations.CacheConcurrencyStrategy;
13+
import org.hibernate.cfg.AvailableSettings;
14+
15+
import org.hibernate.testing.orm.junit.DomainModel;
16+
import org.hibernate.testing.orm.junit.JiraKey;
17+
import org.hibernate.testing.orm.junit.ServiceRegistry;
18+
import org.hibernate.testing.orm.junit.SessionFactory;
19+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
20+
import org.hibernate.testing.orm.junit.Setting;
21+
import org.junit.jupiter.api.BeforeAll;
22+
import org.junit.jupiter.api.Test;
23+
24+
import jakarta.persistence.Column;
25+
import jakarta.persistence.Entity;
26+
import jakarta.persistence.GeneratedValue;
27+
import jakarta.persistence.Id;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
@DomainModel(annotatedClasses = {
32+
QueryCacheIncompleteTest.Admin.class,
33+
})
34+
@SessionFactory
35+
@ServiceRegistry(
36+
settings = {
37+
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"),
38+
@Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "true"),
39+
@Setting(name = AvailableSettings.QUERY_CACHE_LAYOUT, value = "FULL")
40+
}
41+
)
42+
@JiraKey(value = "HHH-18689")
43+
public class QueryCacheIncompleteTest {
44+
45+
private Long adminId;
46+
47+
@BeforeAll
48+
public void setUp(SessionFactoryScope scope) {
49+
adminId = scope.fromTransaction(
50+
session -> {
51+
Admin admin = new Admin();
52+
admin.setAge( 42 );
53+
session.persist( admin );
54+
return admin.getId();
55+
}
56+
);
57+
}
58+
59+
@Test
60+
void testQueryWithEmbeddableParameter(SessionFactoryScope scope) {
61+
scope.inTransaction(
62+
session -> {
63+
// load uninitialized proxy
64+
session.getReference( Admin.class, adminId );
65+
// load entity
66+
var multiLoader = session.byMultipleIds( Admin.class );
67+
multiLoader.with( CacheMode.NORMAL );
68+
multiLoader.multiLoad( adminId );
69+
70+
// store in query cache
71+
Admin admin = queryAdmin( session );
72+
assertThat( admin.getAge() ).isEqualTo( 42 );
73+
}
74+
);
75+
76+
scope.inTransaction(
77+
session -> {
78+
// use query cache
79+
Admin admin = queryAdmin( session );
80+
assertThat( admin.getAge() ).isEqualTo( 42 );
81+
}
82+
);
83+
}
84+
85+
private Admin queryAdmin(Session s) {
86+
return s.createQuery( "from Admin", Admin.class ).setCacheable( true ).getSingleResult();
87+
}
88+
89+
@Entity(name = "Admin")
90+
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
91+
public static class Admin {
92+
93+
@Id
94+
@GeneratedValue
95+
private Long id;
96+
97+
@Column(nullable = false)
98+
private int age;
99+
100+
public Long getId() {
101+
return id;
102+
}
103+
104+
public void setId(Long id) {
105+
this.id = id;
106+
}
107+
108+
public int getAge() {
109+
return age;
110+
}
111+
112+
public void setAge(int age) {
113+
this.age = age;
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)