Skip to content

Commit 55103b8

Browse files
committed
HHH-19206 Test mutating id verifying it doesn't trigger dirty tracking
1 parent 951c819 commit 55103b8

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.bytecode.enhancement.dirty;
8+
9+
import jakarta.persistence.Embeddable;
10+
import jakarta.persistence.EmbeddedId;
11+
import jakarta.persistence.Entity;
12+
13+
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
14+
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
15+
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
16+
import org.hibernate.testing.orm.junit.JiraKey;
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
20+
import java.util.Objects;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
@JiraKey("HHH-19206")
25+
@RunWith(BytecodeEnhancerRunner.class)
26+
@EnhancementOptions(lazyLoading = true, inlineDirtyChecking = true, extendedEnhancement = true)
27+
public class DirtyTrackingIdTest extends BaseNonConfigCoreFunctionalTestCase {
28+
29+
@Override
30+
protected Class[] getAnnotatedClasses() {
31+
return new Class[] {
32+
MyEntity.class
33+
};
34+
}
35+
36+
@Test
37+
public void test() {
38+
inTransaction( session -> {
39+
MyEntity myEntity = new MyEntity();
40+
myEntity.setAnId( new MyEntityId( 1L ) );
41+
myEntity.setData( "initial" );
42+
session.persist( myEntity );
43+
44+
// This is unnecessary, but should be harmless...
45+
// Unfortunately it causes dirty checking to misbehave.
46+
// Comment it, and the test will pass.
47+
myEntity.setAnId( new MyEntityId( 1L ) );
48+
49+
myEntity.setData( "updated" );
50+
} );
51+
inTransaction( session -> {
52+
var entityFromDb = session.find( MyEntity.class, new MyEntityId( 1L ) );
53+
assertThat( entityFromDb.getData() ).isEqualTo( "updated" );
54+
} );
55+
}
56+
57+
// --- //
58+
59+
@Entity(name = "MyEntity")
60+
public static class MyEntity {
61+
// The name of this property must be (alphabetically) before the name of "data" to trigger the bug.
62+
// Yes, it's weird.
63+
@EmbeddedId
64+
private MyEntityId anId;
65+
private String data;
66+
67+
public void setAnId(MyEntityId id) {
68+
this.anId = id;
69+
}
70+
71+
public MyEntityId getAnId() {
72+
return anId;
73+
}
74+
75+
public String getData() {
76+
return data;
77+
}
78+
79+
public void setData(String name) {
80+
this.data = name;
81+
}
82+
}
83+
84+
@Embeddable
85+
public static class MyEntityId {
86+
private Long id;
87+
88+
public MyEntityId() {
89+
}
90+
91+
public MyEntityId(Long id) {
92+
this.id = id;
93+
}
94+
95+
public Long getId() {
96+
return id;
97+
}
98+
99+
public void setId(Long id) {
100+
this.id = id;
101+
}
102+
103+
@Override
104+
public final boolean equals(Object o) {
105+
if ( !(o instanceof MyEntityId that) ) {
106+
return false;
107+
}
108+
109+
return Objects.equals( id, that.id );
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return Objects.hashCode( id );
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)