Skip to content

Commit 1ce5575

Browse files
committed
HHH-18563 Add test for issue
1 parent dfe6a09 commit 1ce5575

File tree

1 file changed

+135
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)