33
33
import static org .hibernate .reactive .testing .DBSelectionExtension .skipTestsFor ;
34
34
35
35
/**
36
- * Adapted from the test with the same name in Hibernate ORM: {@literal org.hibernate.orm.test.rowid.RowIdUpdateTest }
36
+ * Adapted from the test with the same name in Hibernate ORM: {@literal org.hibernate.orm.test.rowid.RowIdUpdateAndDeleteTest }
37
37
*/
38
- public class RowIdUpdateTest extends BaseReactiveTest {
38
+ public class RowIdUpdateAndDeleteTest extends BaseReactiveTest {
39
39
40
40
// Db2: Exception: IllegalStateException: Needed to have 6 in buffer but only had 0
41
41
// Oracle: Vert.x driver doesn't support RowId type parameters
@@ -52,7 +52,7 @@ protected Collection<Class<?>> annotatedEntities() {
52
52
@ Override
53
53
protected Configuration constructConfiguration () {
54
54
Configuration configuration = super .constructConfiguration ();
55
- sqlTracker = new SqlStatementTracker ( RowIdUpdateTest :: isUsingRowId , configuration .getProperties () );
55
+ sqlTracker = new SqlStatementTracker ( RowIdUpdateAndDeleteTest :: isRowIdQuery , configuration .getProperties () );
56
56
return configuration ;
57
57
}
58
58
@@ -61,15 +61,17 @@ protected void addServices(StandardServiceRegistryBuilder builder) {
61
61
sqlTracker .registerService ( builder );
62
62
}
63
63
64
- private static boolean isUsingRowId (String s ) {
65
- return s .toLowerCase ().startsWith ( "update" );
64
+ private static boolean isRowIdQuery (String s ) {
65
+ return s .toLowerCase ().startsWith ( "update" ) || s . toLowerCase (). startsWith ( "delete" ) ;
66
66
}
67
67
68
68
@ BeforeEach
69
69
public void prepareDb (VertxTestContext context ) {
70
70
test ( context , getMutinySessionFactory ().withTransaction ( session -> session .persistAll (
71
71
new SimpleEntity ( 1L , "initial_status" ),
72
- new ParentEntity ( 2L , new SimpleEntity ( 2L , "initial_status" ) )
72
+ new ParentEntity ( 2L , new SimpleEntity ( 2L , "initial_status" ) ),
73
+ new SimpleEntity ( 11L , "to_delete" ),
74
+ new ParentEntity ( 12L , new SimpleEntity ( 12L , "to_delete" ) )
73
75
) ) );
74
76
}
75
77
@@ -86,11 +88,30 @@ public void testSimpleUpdateSameTransaction(VertxTestContext context) {
86
88
.chain ( () -> getMutinySessionFactory ()
87
89
.withSession ( session -> session .find ( SimpleEntity .class , 3L ) ) )
88
90
// the update should have used the primary key, as the row-id value is not available
89
- .invoke ( RowIdUpdateTest :: shouldUsePrimaryKey )
91
+ .invoke ( RowIdUpdateAndDeleteTest :: shouldUsePrimaryKeyForUpdate )
90
92
.invoke ( entity -> assertThat ( entity ).hasFieldOrPropertyWithValue ( "status" , "new_status" ) )
91
93
);
92
94
}
93
95
96
+ @ Test
97
+ public void testSimpleDeleteSameTransaction (VertxTestContext context ) {
98
+ sqlTracker .clear ();
99
+ test ( context , getMutinySessionFactory ()
100
+ .withTransaction ( session -> {
101
+ final SimpleEntity simpleEntity = new SimpleEntity ( 13L , "to_delete" );
102
+ return session .persist ( simpleEntity )
103
+ .call ( session ::flush )
104
+ .call ( () -> session .remove ( simpleEntity ) )
105
+ .invoke ( () -> simpleEntity .setStatus ( "new_status" ) );
106
+ } )
107
+ .chain ( () -> getMutinySessionFactory ()
108
+ .withSession ( session -> session .find ( SimpleEntity .class , 13L ) ) )
109
+ // the update should have used the primary key, as the row-id value is not available
110
+ .invoke ( RowIdUpdateAndDeleteTest ::shouldUsePrimaryKeyForDelete )
111
+ .invoke ( entity -> assertThat ( entity ).isNull () )
112
+ );
113
+ }
114
+
94
115
@ Test
95
116
public void testRelatedUpdateSameTransaction (VertxTestContext context ) {
96
117
sqlTracker .clear ();
@@ -105,11 +126,26 @@ public void testRelatedUpdateSameTransaction(VertxTestContext context) {
105
126
.chain ( () -> getMutinySessionFactory ()
106
127
.withSession ( session -> session .find ( SimpleEntity .class , 4L ) ) )
107
128
// the update should have used the primary key, as the row-id value is not available
108
- .invoke ( RowIdUpdateTest :: shouldUsePrimaryKey )
129
+ .invoke ( RowIdUpdateAndDeleteTest :: shouldUsePrimaryKeyForUpdate )
109
130
.invoke ( entity -> assertThat ( entity ).hasFieldOrPropertyWithValue ( "status" , "new_status" ) )
110
131
);
111
132
}
112
133
134
+ @ Test
135
+ public void testSimpleDeleteDifferentTransaction (VertxTestContext context ) {
136
+ sqlTracker .clear ();
137
+ test ( context , getMutinySessionFactory ()
138
+ .withTransaction ( session -> session
139
+ .find ( SimpleEntity .class , 11L )
140
+ .call ( session ::remove )
141
+ )
142
+ .chain ( () -> getMutinySessionFactory ()
143
+ .withSession ( session -> session .find ( SimpleEntity .class , 11L ) ) )
144
+ .invoke ( RowIdUpdateAndDeleteTest ::shouldUseRowIdForDelete )
145
+ .invoke ( entity -> assertThat ( entity ).isNull () )
146
+ );
147
+ }
148
+
113
149
@ Test
114
150
public void testSimpleUpdateDifferentTransaction (VertxTestContext context ) {
115
151
sqlTracker .clear ();
@@ -120,7 +156,7 @@ public void testSimpleUpdateDifferentTransaction(VertxTestContext context) {
120
156
)
121
157
.chain ( () -> getMutinySessionFactory ()
122
158
.withSession ( session -> session .find ( SimpleEntity .class , 1L ) ) )
123
- .invoke ( RowIdUpdateTest :: shouldUseRowId )
159
+ .invoke ( RowIdUpdateAndDeleteTest :: shouldUseRowIdForUpdate )
124
160
.invoke ( entity -> assertThat ( entity ).hasFieldOrPropertyWithValue ( "status" , "new_status" ) )
125
161
);
126
162
}
@@ -133,7 +169,7 @@ public void testRelatedUpdateRelatedDifferentTransaction(VertxTestContext contex
133
169
.find ( ParentEntity .class , 2L )
134
170
.invoke ( entity -> entity .getChild ().setStatus ( "new_status" ) )
135
171
)
136
- .invoke ( RowIdUpdateTest :: shouldUseRowId )
172
+ .invoke ( RowIdUpdateAndDeleteTest :: shouldUseRowIdForUpdate )
137
173
.chain ( () -> getMutinySessionFactory ()
138
174
.withSession ( session -> session .find ( SimpleEntity .class , 2L ) ) )
139
175
.invoke ( entity -> assertThat ( entity )
@@ -142,13 +178,19 @@ public void testRelatedUpdateRelatedDifferentTransaction(VertxTestContext contex
142
178
);
143
179
}
144
180
145
- private static void shouldUsePrimaryKey () {
181
+ private static void shouldUsePrimaryKeyForUpdate () {
146
182
assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
147
183
assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) )
148
184
.matches ( "update SimpleEntity set status=.+ where primary_key=.+" );
149
185
}
150
186
151
- private static void shouldUseRowId () {
187
+ private static void shouldUsePrimaryKeyForDelete () {
188
+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
189
+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) )
190
+ .matches ( "delete from SimpleEntity where primary_key=.+" );
191
+ }
192
+
193
+ private static void shouldUseRowIdForUpdate () {
152
194
// Not all databases have a rowId column
153
195
String rowId = getDialect ().rowId ( "" );
154
196
String column = rowId == null ? "primary_key" : rowId ;
@@ -157,6 +199,15 @@ private static void shouldUseRowId() {
157
199
.matches ( "update SimpleEntity set status=.+ where " + column + "=.+" );
158
200
}
159
201
202
+ private static void shouldUseRowIdForDelete () {
203
+ // Not all databases have a rowId column
204
+ String rowId = getDialect ().rowId ( "" );
205
+ String column = rowId == null ? "primary_key" : rowId ;
206
+ assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
207
+ assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) )
208
+ .matches ( "delete from SimpleEntity where " + column + "=.+" );
209
+ }
210
+
160
211
@ Entity (name = "SimpleEntity" )
161
212
@ Table (name = "SimpleEntity" )
162
213
@ RowId
0 commit comments