Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 15dc518

Browse files
committed
HHH-19605 Add test for issue
1 parent be72098 commit 15dc518

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.batch;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.JoinColumn;
10+
import jakarta.persistence.ManyToOne;
11+
import org.hibernate.annotations.Cache;
12+
import org.hibernate.annotations.CacheConcurrencyStrategy;
13+
import org.hibernate.cache.spi.CacheImplementor;
14+
import org.hibernate.cfg.AvailableSettings;
15+
import org.hibernate.testing.orm.junit.DomainModel;
16+
import org.hibernate.testing.orm.junit.Jira;
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.AfterAll;
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.util.List;
26+
27+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
28+
29+
@DomainModel(annotatedClasses = {
30+
BatchCacheCircularAssociationTest.EntityA.class,
31+
BatchCacheCircularAssociationTest.EntityB.class
32+
})
33+
@ServiceRegistry(settings = {
34+
@Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "5"),
35+
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"),
36+
})
37+
@SessionFactory
38+
@Jira("https://hibernate.atlassian.net/browse/HHH-19605")
39+
public class BatchCacheCircularAssociationTest {
40+
@Test
41+
public void testDirtiness(SessionFactoryScope scope) {
42+
final CacheImplementor cache = scope.getSessionFactory().getCache();
43+
cache.evictAllRegions();
44+
scope.inTransaction( session -> {
45+
final List<EntityA> resultList = session.createSelectionQuery(
46+
"select a from EntityA a order by a.id",
47+
EntityA.class
48+
).getResultList();
49+
assertThat( resultList ).hasSize( 2 );
50+
assertThat( session.isDirty() ).isFalse();
51+
52+
final EntityA entityA1 = resultList.get( 0 );
53+
assertThat( entityA1.getId() ).isEqualTo( 1L );
54+
assertThat( entityA1.getName() ).isEqualTo( "A1" );
55+
assertThat( entityA1.getEntityB() ).isNull();
56+
57+
final EntityA entityA2 = resultList.get( 1 );
58+
assertThat( entityA2.getId() ).isEqualTo( 2L );
59+
assertThat( entityA2.getName() ).isEqualTo( "A2" );
60+
assertThat( entityA2.getEntityB() ).isNotNull();
61+
assertThat( entityA2.getEntityB().getEntityA() ).isSameAs( entityA1 );
62+
} );
63+
}
64+
65+
@BeforeAll
66+
public void setUp(SessionFactoryScope scope) {
67+
scope.inTransaction( session -> {
68+
final EntityA entityA1 = new EntityA( 1L, "A1" );
69+
final EntityA entityA2 = new EntityA( 2L, "A2" );
70+
final EntityB entityB = new EntityB( 1L, "B1" );
71+
entityB.entityA = entityA1;
72+
entityA2.entityB = entityB;
73+
session.persist( entityA1 );
74+
session.persist( entityA2 );
75+
session.persist( entityB );
76+
} );
77+
}
78+
79+
@AfterAll
80+
public void tearDown(SessionFactoryScope scope) {
81+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
82+
}
83+
84+
@Entity(name = "EntityA")
85+
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
86+
static class EntityA {
87+
@Id
88+
Long id;
89+
90+
String name;
91+
92+
@ManyToOne
93+
@JoinColumn(name = "entity_b")
94+
EntityB entityB;
95+
96+
public EntityA() {
97+
}
98+
99+
public EntityA(Long id, String name) {
100+
this.id = id;
101+
this.name = name;
102+
}
103+
104+
public Long getId() {
105+
return id;
106+
}
107+
108+
public String getName() {
109+
return name;
110+
}
111+
112+
public EntityB getEntityB() {
113+
return entityB;
114+
}
115+
}
116+
117+
@Entity(name = "EntityB")
118+
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
119+
static class EntityB {
120+
@Id
121+
Long id;
122+
123+
String name;
124+
125+
@ManyToOne
126+
@JoinColumn(name = "entity_a")
127+
EntityA entityA;
128+
129+
public EntityB() {
130+
}
131+
132+
public EntityB(Long id, String name) {
133+
this.id = id;
134+
this.name = name;
135+
}
136+
137+
public Long getId() {
138+
return id;
139+
}
140+
141+
public String getName() {
142+
return name;
143+
}
144+
145+
public EntityA getEntityA() {
146+
return entityA;
147+
}
148+
}
149+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /