Skip to content

Commit 2a62f89

Browse files
committedMar 3, 2025
HHH-19206 Test mutating id verifying it doesn't trigger dirty tracking
1 parent 65f8d91 commit 2a62f89

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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.Embeddable;
8+
import jakarta.persistence.EmbeddedId;
9+
import jakarta.persistence.Entity;
10+
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
11+
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.JiraKey;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.Test;
17+
18+
import java.util.Objects;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
@JiraKey("HHH-19206")
23+
@DomainModel(
24+
annotatedClasses = {
25+
DirtyTrackingIdTest.MyEntity.class
26+
}
27+
)
28+
@SessionFactory
29+
@BytecodeEnhanced
30+
@EnhancementOptions(lazyLoading = true, inlineDirtyChecking = true, extendedEnhancement = true)
31+
public class DirtyTrackingIdTest {
32+
33+
@Test
34+
public void test(SessionFactoryScope scope) {
35+
scope.inTransaction( session -> {
36+
MyEntity myEntity = new MyEntity();
37+
myEntity.setAnId( new MyEntityId( 1L ) );
38+
myEntity.setData( "initial" );
39+
session.persist( myEntity );
40+
41+
// This is unnecessary, but should be harmless...
42+
// Unfortunately it causes dirty checking to misbehave.
43+
// Comment it, and the test will pass.
44+
myEntity.setAnId( new MyEntityId( 1L ) );
45+
46+
myEntity.setData( "updated" );
47+
} );
48+
scope.inTransaction( session -> {
49+
var entityFromDb = session.find( MyEntity.class, new MyEntityId( 1L ) );
50+
assertThat( entityFromDb.getData() ).isEqualTo( "updated" );
51+
} );
52+
}
53+
54+
// --- //
55+
56+
@Entity(name = "MyEntity")
57+
public static class MyEntity {
58+
// The name of this property must be (alphabetically) before the name of "data" to trigger the bug.
59+
// Yes, it's weird.
60+
@EmbeddedId
61+
private MyEntityId anId;
62+
private String data;
63+
64+
public void setAnId(MyEntityId id) {
65+
this.anId = id;
66+
}
67+
68+
public MyEntityId getAnId() {
69+
return anId;
70+
}
71+
72+
public String getData() {
73+
return data;
74+
}
75+
76+
public void setData(String name) {
77+
this.data = name;
78+
}
79+
}
80+
81+
@Embeddable
82+
public static class MyEntityId {
83+
private Long id;
84+
85+
public MyEntityId() {
86+
}
87+
88+
public MyEntityId(Long id) {
89+
this.id = id;
90+
}
91+
92+
public Long getId() {
93+
return id;
94+
}
95+
96+
public void setId(Long id) {
97+
this.id = id;
98+
}
99+
100+
@Override
101+
public final boolean equals(Object o) {
102+
if ( !(o instanceof MyEntityId) ) {
103+
return false;
104+
}
105+
106+
return Objects.equals( id, ( (MyEntityId) o ).id );
107+
}
108+
109+
@Override
110+
public int hashCode() {
111+
return Objects.hashCode( id );
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)