Skip to content

Commit dd0b1b2

Browse files
committed
HHH-14244 Add test showing the issue has been solved
1 parent 045b722 commit dd0b1b2

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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.bytecode.enhancement.dirty;
6+
7+
import jakarta.persistence.Basic;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.FetchType;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.Hibernate;
12+
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
13+
import org.hibernate.testing.orm.junit.DomainModel;
14+
import org.hibernate.testing.orm.junit.Jira;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.junit.jupiter.api.AfterEach;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
22+
23+
@Jira("HHH-14244")
24+
@DomainModel(
25+
annotatedClasses = {
26+
BasicLazyPropertyUpdateTest.TestEntity.class
27+
}
28+
)
29+
@SessionFactory
30+
@BytecodeEnhanced(testEnhancedClasses = BasicLazyPropertyUpdateTest.TestEntity.class)
31+
public class BasicLazyPropertyUpdateTest {
32+
33+
private static final Long ID = 1L;
34+
private static final String NEW_ENTITY_NAME = "new name";
35+
36+
@BeforeEach
37+
public void setUp(SessionFactoryScope scope) {
38+
scope.inTransaction(
39+
session -> {
40+
TestEntity testEntity = new TestEntity( ID, "name", "lazy value" );
41+
session.persist( testEntity );
42+
}
43+
);
44+
}
45+
46+
@AfterEach
47+
public void cleanup(SessionFactoryScope scope) {
48+
scope.inTransaction(
49+
session -> {
50+
session.createMutationQuery( "delete from TestEntity" ).executeUpdate();
51+
}
52+
);
53+
}
54+
55+
@Test
56+
public void testUpdateNonLazyProperty(SessionFactoryScope scope) {
57+
58+
TestEntity testEntity = scope.fromTransaction(
59+
session -> {
60+
TestEntity entity = session.find( TestEntity.class, ID );
61+
assertThatLazyPropertyIsNotInitialized( entity );
62+
entity.setName( NEW_ENTITY_NAME );
63+
return entity;
64+
}
65+
);
66+
67+
assertThatLazyPropertyIsNotInitialized( testEntity );
68+
69+
scope.inTransaction(
70+
session -> {
71+
TestEntity entity = session.find( TestEntity.class, ID );
72+
assertThat( entity.getName() ).isEqualTo( NEW_ENTITY_NAME );
73+
}
74+
);
75+
}
76+
77+
@Test
78+
public void testUpdateLazyProperty(SessionFactoryScope scope) {
79+
String newLazyPropertyValue = "changed";
80+
81+
TestEntity testEntity = scope.fromTransaction(
82+
session -> {
83+
TestEntity entity = session.find( TestEntity.class, ID );
84+
assertThatLazyPropertyIsNotInitialized( entity );
85+
entity.setLazyProperty( newLazyPropertyValue );
86+
return entity;
87+
}
88+
);
89+
90+
assertThatLazyPropertyIsInitialized( testEntity );
91+
92+
assertThatLazyFieldIsEqualTo( newLazyPropertyValue, scope );
93+
}
94+
95+
@Test
96+
public void testUpdateLazyPropertyToNull(SessionFactoryScope scope) {
97+
TestEntity testEntity =scope.fromTransaction(
98+
session -> {
99+
TestEntity entity = session.find( TestEntity.class, ID );
100+
assertThatLazyPropertyIsNotInitialized( entity );
101+
entity.setLazyProperty( null );
102+
return entity;
103+
}
104+
);
105+
106+
assertThatLazyPropertyIsInitialized( testEntity );
107+
108+
109+
assertThatLazyFieldIsEqualTo( null, scope );
110+
}
111+
112+
private static void assertThatLazyPropertyIsInitialized(TestEntity testEntity) {
113+
assertThat( Hibernate.isPropertyInitialized( testEntity, "lazyProperty" ) ).as( "Lazy property has not been initialized" ).isTrue();
114+
}
115+
116+
private static void assertThatLazyPropertyIsNotInitialized(TestEntity entity) {
117+
assertThat( Hibernate.isPropertyInitialized( entity, "lazyProperty" ) ).as( "Lazy property has been initialized" ).isFalse();
118+
}
119+
120+
private void assertThatLazyFieldIsEqualTo(String expected, SessionFactoryScope scope) {
121+
scope.inTransaction(
122+
session -> {
123+
TestEntity testEntity = session.find( TestEntity.class, ID );
124+
assertThatLazyPropertyIsNotInitialized( testEntity );
125+
assertThat( testEntity.getLazyProperty() ).isEqualTo(expected);
126+
assertThatLazyPropertyIsInitialized( testEntity );
127+
}
128+
);
129+
}
130+
131+
@Entity(name= "TestEntity")
132+
public static class TestEntity {
133+
@Id
134+
private Long id;
135+
136+
private String name;
137+
138+
@Basic(fetch = FetchType.LAZY)
139+
private String lazyProperty;
140+
141+
public TestEntity() {
142+
}
143+
144+
public TestEntity(Long id, String name, String lazyProperty) {
145+
this.id = id;
146+
this.name = name;
147+
this.lazyProperty = lazyProperty;
148+
}
149+
150+
public void setName(String name) {
151+
this.name = name;
152+
}
153+
154+
public void setLazyProperty(String lazyProperty) {
155+
this.lazyProperty = lazyProperty;
156+
}
157+
158+
public Long getId() {
159+
return id;
160+
}
161+
162+
public String getName() {
163+
return name;
164+
}
165+
166+
public String getLazyProperty() {
167+
return lazyProperty;
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)