Skip to content

Commit e04b8f3

Browse files
marko-bekhtambellade
authored andcommitted
HHH-19857 Add test for issue
1 parent a3ad85d commit e04b8f3

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.bytecode.enhancement.lazy;
6+
7+
import jakarta.persistence.Basic;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.FetchType;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.JoinColumn;
12+
import jakarta.persistence.OneToOne;
13+
import org.hibernate.Hibernate;
14+
import org.hibernate.annotations.LazyGroup;
15+
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
16+
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.SessionFactory;
19+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
@DomainModel(annotatedClasses = {
26+
LazyOneToOneWithCastTest.EntityA.class,
27+
LazyOneToOneWithCastTest.EntityB.class,
28+
})
29+
@SessionFactory
30+
@BytecodeEnhanced
31+
@EnhancementOptions(lazyLoading = true)
32+
public class LazyOneToOneWithCastTest {
33+
34+
@Test
35+
void oneNullOneNotNull(SessionFactoryScope scope) {
36+
scope.inTransaction( session -> {
37+
final EntityA entityA = new EntityA();
38+
entityA.setId( 1 );
39+
40+
final EntityB entityB = new EntityB();
41+
entityB.setId( 2 );
42+
entityB.setToOneWithCast( entityA );
43+
entityA.setToOneWithCast( entityB );
44+
entityB.setLazyString( "lazy" );
45+
46+
session.persist( entityB );
47+
session.persist( entityA );
48+
49+
} );
50+
51+
scope.inTransaction( session -> {
52+
final EntityB entityB = session.find( EntityB.class, 2 );
53+
54+
EntityA toOne = entityB.getToOne();
55+
assertThat( toOne ).isNull();
56+
assertThat( Hibernate.isPropertyInitialized( entityB, "toOne" ) ).isTrue();
57+
assertThat( Hibernate.isPropertyInitialized( entityB, "toOneWithCast" ) ).isFalse();
58+
assertThat( Hibernate.isPropertyInitialized( entityB, "lazyString" ) ).isFalse();
59+
60+
// update lazy-basic property in same lazy-group
61+
entityB.setLazyString( "lazy_updated" );
62+
assertThat( Hibernate.isPropertyInitialized( entityB, "lazyString" ) ).isTrue();
63+
assertThat( Hibernate.isPropertyInitialized( entityB, "toOneWithCast" ) ).isFalse();
64+
65+
// now access the lazy to-one with cast
66+
final EntityA toOneWithCast = entityB.getToOneWithCast();
67+
assertThat( Hibernate.isPropertyInitialized( entityB, "toOneWithCast" ) ).isTrue();
68+
assertThat( Hibernate.isPropertyInitialized( entityB, "lazyString" ) ).isTrue();
69+
assertThat( toOneWithCast ).isNotNull().extracting( EntityA::getId ).isEqualTo( 1 );
70+
assertThat( entityB.getLazyString() ).isEqualTo( "lazy_updated" );
71+
} );
72+
}
73+
74+
@AfterEach
75+
void tearDown(SessionFactoryScope scope) {
76+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
77+
}
78+
79+
@Entity(name = "EntityA")
80+
static class EntityA {
81+
@Id
82+
private Integer id;
83+
84+
@OneToOne
85+
@JoinColumn(name = "entity_b_id")
86+
private EntityB toOne;
87+
88+
@OneToOne(targetEntity = EntityB.class)
89+
@JoinColumn(name = "entity_b_id_cast")
90+
private EntityB toOneWithCast;
91+
92+
public Integer getId() {
93+
return id;
94+
}
95+
96+
public void setId(Integer id) {
97+
this.id = id;
98+
}
99+
100+
public EntityB getToOne() {
101+
return toOne;
102+
}
103+
104+
public void setToOne(EntityB containedIndexedEmbedded) {
105+
this.toOne = containedIndexedEmbedded;
106+
}
107+
108+
public Object getToOneWithCast() {
109+
return toOneWithCast;
110+
}
111+
112+
public void setToOneWithCast(EntityB containedIndexedEmbeddedWithCast) {
113+
this.toOneWithCast = containedIndexedEmbeddedWithCast;
114+
}
115+
}
116+
117+
@Entity(name = "EntityB")
118+
static class EntityB {
119+
@Id
120+
private Integer id;
121+
122+
@OneToOne(mappedBy = "toOne", fetch = FetchType.LAZY)
123+
@LazyGroup("toOneGroup")
124+
private EntityA toOne;
125+
126+
@OneToOne(mappedBy = "toOneWithCast", targetEntity = EntityA.class, fetch = FetchType.LAZY)
127+
@LazyGroup("toOneWithCastGroup")
128+
private EntityA toOneWithCast;
129+
130+
@Basic(fetch = FetchType.LAZY)
131+
@LazyGroup("toOneWithCastGroup")
132+
private String lazyString;
133+
134+
public Integer getId() {
135+
return id;
136+
}
137+
138+
public void setId(Integer id) {
139+
this.id = id;
140+
}
141+
142+
public EntityA getToOne() {
143+
return toOne;
144+
}
145+
146+
public void setToOne(EntityA toOne) {
147+
this.toOne = toOne;
148+
}
149+
150+
public EntityA getToOneWithCast() {
151+
return toOneWithCast;
152+
}
153+
154+
public void setToOneWithCast(EntityA toOneWithCast) {
155+
this.toOneWithCast = toOneWithCast;
156+
}
157+
158+
public String getLazyString() {
159+
return lazyString;
160+
}
161+
162+
public void setLazyString(String lazyString) {
163+
this.lazyString = lazyString;
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)