Skip to content

Commit e9823c8

Browse files
dreab8beikov
authored andcommitted
HHH-18563 Add test for issue
1 parent 8ebd5a7 commit e9823c8

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package org.hibernate.orm.test.flush;
2+
3+
import org.hibernate.testing.jdbc.SQLStatementInspector;
4+
import org.hibernate.testing.orm.junit.DomainModel;
5+
import org.hibernate.testing.orm.junit.SessionFactory;
6+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
import jakarta.persistence.Entity;
12+
import jakarta.persistence.GeneratedValue;
13+
import jakarta.persistence.Id;
14+
import jakarta.persistence.OneToOne;
15+
16+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
17+
18+
19+
@DomainModel(
20+
annotatedClasses = {
21+
AutoFlushOnUpdateQueryTest.FruitLogEntry.class,
22+
AutoFlushOnUpdateQueryTest.Fruit.class,
23+
}
24+
)
25+
@SessionFactory(
26+
statementInspectorClass = SQLStatementInspector.class
27+
)
28+
public class AutoFlushOnUpdateQueryTest {
29+
30+
public static final String FRUIT_NAME = "Apple";
31+
32+
@BeforeEach
33+
public void setUp(SessionFactoryScope scope) {
34+
scope.inTransaction(
35+
session -> {
36+
session.persist( new Fruit( FRUIT_NAME ) );
37+
}
38+
);
39+
}
40+
41+
@AfterEach
42+
public void tearDown(SessionFactoryScope scope) {
43+
scope.inTransaction(
44+
session -> {
45+
session.createMutationQuery( "delete from Fruit" ).executeUpdate();
46+
session.createMutationQuery( "delete from FruitLogEntry" ).executeUpdate();
47+
}
48+
);
49+
}
50+
51+
@Test
52+
public void testFlushIsExecuted(SessionFactoryScope scope) {
53+
SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
54+
statementInspector.clear();
55+
scope.inTransaction(
56+
session -> {
57+
Fruit fruit = session
58+
.createQuery(
59+
"select f from Fruit f where f.name = :name",
60+
Fruit.class
61+
).setParameter( "name", FRUIT_NAME ).getSingleResult();
62+
63+
FruitLogEntry logEntry = new FruitLogEntry( fruit, "foo" );
64+
session.persist( logEntry );
65+
66+
session.createMutationQuery( "update Fruit f set f.logEntry = :logEntry where f.id = :fruitId" )
67+
.setParameter( "logEntry", logEntry )
68+
.setParameter( "fruitId", fruit.getId() ).executeUpdate();
69+
}
70+
);
71+
72+
scope.inTransaction(
73+
session -> {
74+
Fruit fruit = session
75+
.createQuery(
76+
"select f from Fruit f where f.name = :name",
77+
Fruit.class
78+
).setParameter( "name", FRUIT_NAME ).getSingleResult();
79+
assertThat( fruit.getLogEntry() ).isNotNull();
80+
}
81+
);
82+
}
83+
84+
@Entity(name = "Fruit")
85+
public static class Fruit {
86+
87+
@Id
88+
@GeneratedValue
89+
private Long id;
90+
91+
private String name;
92+
93+
@OneToOne
94+
private FruitLogEntry logEntry;
95+
96+
public Fruit() {
97+
}
98+
99+
public Fruit(String name) {
100+
this.name = name;
101+
}
102+
103+
public Long getId() {
104+
return id;
105+
}
106+
107+
public String getName() {
108+
return name;
109+
}
110+
111+
public FruitLogEntry getLogEntry() {
112+
return logEntry;
113+
}
114+
}
115+
116+
@Entity(name = "FruitLogEntry")
117+
public static class FruitLogEntry {
118+
119+
@Id
120+
@GeneratedValue
121+
private Long id;
122+
123+
@OneToOne(mappedBy = "logEntry")
124+
private Fruit fruit;
125+
126+
private String logComments;
127+
128+
public FruitLogEntry(Fruit fruit, String comment) {
129+
this.fruit = fruit;
130+
this.logComments = comment;
131+
}
132+
133+
FruitLogEntry() {
134+
}
135+
136+
public Long getId() {
137+
return id;
138+
}
139+
140+
public Fruit getFruit() {
141+
return fruit;
142+
}
143+
144+
public String getLogComments() {
145+
return logComments;
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)