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 c50b13d

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

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.dirtiness;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.FetchType;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.JoinColumn;
11+
import jakarta.persistence.ManyToOne;
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.annotations.Cache;
14+
import org.hibernate.annotations.CacheConcurrencyStrategy;
15+
import org.hibernate.cache.spi.CacheImplementor;
16+
import org.hibernate.cfg.AvailableSettings;
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.Jira;
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.AfterAll;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.util.List;
28+
29+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
30+
31+
@DomainModel(annotatedClasses = {
32+
SessionIsDirtyTests.EntityA.class,
33+
SessionIsDirtyTests.EntityB.class,
34+
SessionIsDirtyTests.EntityC.class,
35+
})
36+
@ServiceRegistry(settings = {
37+
@Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "5"),
38+
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"),
39+
})
40+
@SessionFactory
41+
@Jira("https://hibernate.atlassian.net/browse/HHH-19605")
42+
public class SessionIsDirtyTests {
43+
@Test
44+
public void testBatchAndCacheDirtiness(SessionFactoryScope scope) {
45+
final CacheImplementor cache = scope.getSessionFactory().getCache();
46+
cache.evictAllRegions();
47+
scope.inTransaction( session -> {
48+
final List<EntityA> resultList = session.createSelectionQuery(
49+
"select a from EntityA a order by a.id",
50+
EntityA.class
51+
).getResultList();
52+
assertThat( session.isDirty() ).isFalse();
53+
54+
assertThat( resultList ).hasSize( 2 );
55+
final EntityA entityA1 = resultList.get( 0 );
56+
assertThat( entityA1.getId() ).isEqualTo( 1L );
57+
assertThat( entityA1.getName() ).isEqualTo( "A1" );
58+
assertThat( entityA1.getEntityB() ).isNull();
59+
60+
final EntityA entityA2 = resultList.get( 1 );
61+
assertThat( entityA2.getId() ).isEqualTo( 2L );
62+
assertThat( entityA2.getName() ).isEqualTo( "A2" );
63+
assertThat( entityA2.getEntityB() ).isNotNull();
64+
assertThat( entityA2.getEntityB().getEntityA() ).isSameAs( entityA1 );
65+
} );
66+
}
67+
68+
@Test
69+
public void testLazyAssociationDirtiness(SessionFactoryScope scope) {
70+
scope.inTransaction( session -> {
71+
final List<EntityC> resultList = session.createSelectionQuery(
72+
"select c from EntityC c order by c.id",
73+
EntityC.class
74+
).getResultList();
75+
assertThat( session.isDirty() ).isFalse();
76+
77+
assertThat( resultList ).hasSize( 1 );
78+
final EntityC entityC = resultList.get( 0 );
79+
assertThat( entityC.getId() ).isEqualTo( 1L );
80+
assertThat( entityC.getName() ).isEqualTo( "C1" );
81+
assertThat( Hibernate.isInitialized( entityC.getEntityB() ) ).isFalse();
82+
} );
83+
}
84+
85+
@BeforeAll
86+
public void setUp(SessionFactoryScope scope) {
87+
scope.inTransaction( session -> {
88+
final EntityA entityA1 = new EntityA( 1L, "A1" );
89+
final EntityA entityA2 = new EntityA( 2L, "A2" );
90+
final EntityB entityB = new EntityB( 1L, "B1" );
91+
entityB.entityA = entityA1;
92+
entityA2.entityB = entityB;
93+
session.persist( entityA1 );
94+
session.persist( entityA2 );
95+
session.persist( entityB );
96+
97+
final EntityC entityC = new EntityC( 1L, "C1" );
98+
entityC.entityB = entityB;
99+
session.persist( entityC );
100+
} );
101+
}
102+
103+
@AfterAll
104+
public void tearDown(SessionFactoryScope scope) {
105+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
106+
}
107+
108+
@Entity(name = "EntityA")
109+
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
110+
static class EntityA {
111+
@Id
112+
Long id;
113+
114+
String name;
115+
116+
@ManyToOne
117+
@JoinColumn(name = "entity_b")
118+
EntityB entityB;
119+
120+
public EntityA() {
121+
}
122+
123+
public EntityA(Long id, String name) {
124+
this.id = id;
125+
this.name = name;
126+
}
127+
128+
public Long getId() {
129+
return id;
130+
}
131+
132+
public String getName() {
133+
return name;
134+
}
135+
136+
public EntityB getEntityB() {
137+
return entityB;
138+
}
139+
}
140+
141+
@Entity(name = "EntityB")
142+
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
143+
static class EntityB {
144+
@Id
145+
Long id;
146+
147+
String name;
148+
149+
@ManyToOne
150+
@JoinColumn(name = "entity_a")
151+
EntityA entityA;
152+
153+
public EntityB() {
154+
}
155+
156+
public EntityB(Long id, String name) {
157+
this.id = id;
158+
this.name = name;
159+
}
160+
161+
public Long getId() {
162+
return id;
163+
}
164+
165+
public String getName() {
166+
return name;
167+
}
168+
169+
public EntityA getEntityA() {
170+
return entityA;
171+
}
172+
}
173+
174+
@Entity(name = "EntityC")
175+
static class EntityC {
176+
@Id
177+
Long id;
178+
179+
String name;
180+
181+
@ManyToOne(fetch = FetchType.LAZY)
182+
@JoinColumn(name = "entity_b")
183+
EntityB entityB;
184+
185+
public EntityC() {
186+
}
187+
188+
public EntityC(Long id, String name) {
189+
this.id = id;
190+
this.name = name;
191+
}
192+
193+
public Long getId() {
194+
return id;
195+
}
196+
197+
public String getName() {
198+
return name;
199+
}
200+
201+
public EntityB getEntityB() {
202+
return entityB;
203+
}
204+
}
205+
}

0 commit comments

Comments
(0)

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