Skip to content

Commit ddf0cb7

Browse files
committed
HHH-15045 Add test for issue
1 parent 15c4b24 commit ddf0cb7

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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.onetoone.flush;
8+
9+
import org.hibernate.testing.TestForIssue;
10+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
11+
import org.hibernate.testing.orm.junit.Jpa;
12+
import org.junit.jupiter.api.AfterEach;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
16+
import jakarta.persistence.Entity;
17+
import jakarta.persistence.Id;
18+
import jakarta.persistence.OneToOne;
19+
import jakarta.persistence.Table;
20+
import jakarta.persistence.Version;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
24+
@TestForIssue(jiraKey = "HHH-15045")
25+
@Jpa(
26+
annotatedClasses = {
27+
DirtyFlushTest.User.class,
28+
DirtyFlushTest.Profile.class
29+
}
30+
)
31+
public class DirtyFlushTest {
32+
33+
@BeforeEach
34+
public void setUp(EntityManagerFactoryScope scope) {
35+
scope.inTransaction( em -> {
36+
final User user = new User();
37+
user.id = 1;
38+
user.version = 1;
39+
40+
final Profile profile = new Profile();
41+
profile.id = 1;
42+
profile.version = 1;
43+
44+
em.persist( user );
45+
em.persist( profile );
46+
} );
47+
}
48+
49+
@Test
50+
public void testDirtyFlushNotHappened(EntityManagerFactoryScope scope) {
51+
scope.inTransaction( em -> {
52+
final User user = em.find( User.class, 1 );
53+
assertEquals( 1, user.version );
54+
55+
final Profile profile = em.find( Profile.class, 1 );
56+
assertEquals( 1, profile.version );
57+
58+
profile.user = user;
59+
user.profile = profile;
60+
61+
em.persist( profile );
62+
em.flush();
63+
} );
64+
65+
scope.inTransaction( em -> {
66+
final Profile profile = em.find( Profile.class, 1 );
67+
assertEquals( 2, profile.version );
68+
69+
final User user = em.find( User.class, 1 );
70+
assertEquals(
71+
1,
72+
user.version,
73+
"without fixing, the version will be bumped due to erroneous dirty flushing"
74+
);
75+
} );
76+
}
77+
78+
@AfterEach
79+
public void tearDown(EntityManagerFactoryScope scope) {
80+
scope.inTransaction( em -> {
81+
em.createQuery( "delete from Profile" ).executeUpdate();
82+
em.createQuery( "delete from User" ).executeUpdate();
83+
} );
84+
}
85+
86+
@Entity(name = "User")
87+
@Table(name = "USER_TABLE")
88+
public static class User {
89+
@Id
90+
int id;
91+
92+
@Version
93+
int version;
94+
95+
@OneToOne(mappedBy = "user")
96+
Profile profile;
97+
}
98+
99+
@Entity(name = "Profile")
100+
@Table(name = "PROFILE_TABLE")
101+
public static class Profile {
102+
@Id
103+
int id;
104+
105+
@Version
106+
int version;
107+
@OneToOne // internally Hibernate will use `@ManyToOne` for this field
108+
User user;
109+
}
110+
}

0 commit comments

Comments
 (0)