3333import static org .hibernate .reactive .testing .DBSelectionExtension .skipTestsFor ;
3434
3535/**
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 }
3737 */
38- public class RowIdUpdateTest extends BaseReactiveTest {
38+ public class RowIdUpdateAndDeleteTest extends BaseReactiveTest {
3939
4040 // Db2: Exception: IllegalStateException: Needed to have 6 in buffer but only had 0
4141 // Oracle: Vert.x driver doesn't support RowId type parameters
@@ -52,7 +52,7 @@ protected Collection<Class<?>> annotatedEntities() {
5252 @ Override
5353 protected Configuration constructConfiguration () {
5454 Configuration configuration = super .constructConfiguration ();
55- sqlTracker = new SqlStatementTracker ( RowIdUpdateTest :: isUsingRowId , configuration .getProperties () );
55+ sqlTracker = new SqlStatementTracker ( RowIdUpdateAndDeleteTest :: isRowIdQuery , configuration .getProperties () );
5656 return configuration ;
5757 }
5858
@@ -61,15 +61,17 @@ protected void addServices(StandardServiceRegistryBuilder builder) {
6161 sqlTracker .registerService ( builder );
6262 }
6363
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" ) ;
6666 }
6767
6868 @ BeforeEach
6969 public void prepareDb (VertxTestContext context ) {
7070 test ( context , getMutinySessionFactory ().withTransaction ( session -> session .persistAll (
7171 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" ) )
7375 ) ) );
7476 }
7577
@@ -86,11 +88,30 @@ public void testSimpleUpdateSameTransaction(VertxTestContext context) {
8688 .chain ( () -> getMutinySessionFactory ()
8789 .withSession ( session -> session .find ( SimpleEntity .class , 3L ) ) )
8890 // the update should have used the primary key, as the row-id value is not available
89- .invoke ( RowIdUpdateTest :: shouldUsePrimaryKey )
91+ .invoke ( RowIdUpdateAndDeleteTest :: shouldUsePrimaryKeyForUpdate )
9092 .invoke ( entity -> assertThat ( entity ).hasFieldOrPropertyWithValue ( "status" , "new_status" ) )
9193 );
9294 }
9395
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+
94115 @ Test
95116 public void testRelatedUpdateSameTransaction (VertxTestContext context ) {
96117 sqlTracker .clear ();
@@ -105,11 +126,26 @@ public void testRelatedUpdateSameTransaction(VertxTestContext context) {
105126 .chain ( () -> getMutinySessionFactory ()
106127 .withSession ( session -> session .find ( SimpleEntity .class , 4L ) ) )
107128 // the update should have used the primary key, as the row-id value is not available
108- .invoke ( RowIdUpdateTest :: shouldUsePrimaryKey )
129+ .invoke ( RowIdUpdateAndDeleteTest :: shouldUsePrimaryKeyForUpdate )
109130 .invoke ( entity -> assertThat ( entity ).hasFieldOrPropertyWithValue ( "status" , "new_status" ) )
110131 );
111132 }
112133
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+
113149 @ Test
114150 public void testSimpleUpdateDifferentTransaction (VertxTestContext context ) {
115151 sqlTracker .clear ();
@@ -120,7 +156,7 @@ public void testSimpleUpdateDifferentTransaction(VertxTestContext context) {
120156 )
121157 .chain ( () -> getMutinySessionFactory ()
122158 .withSession ( session -> session .find ( SimpleEntity .class , 1L ) ) )
123- .invoke ( RowIdUpdateTest :: shouldUseRowId )
159+ .invoke ( RowIdUpdateAndDeleteTest :: shouldUseRowIdForUpdate )
124160 .invoke ( entity -> assertThat ( entity ).hasFieldOrPropertyWithValue ( "status" , "new_status" ) )
125161 );
126162 }
@@ -133,7 +169,7 @@ public void testRelatedUpdateRelatedDifferentTransaction(VertxTestContext contex
133169 .find ( ParentEntity .class , 2L )
134170 .invoke ( entity -> entity .getChild ().setStatus ( "new_status" ) )
135171 )
136- .invoke ( RowIdUpdateTest :: shouldUseRowId )
172+ .invoke ( RowIdUpdateAndDeleteTest :: shouldUseRowIdForUpdate )
137173 .chain ( () -> getMutinySessionFactory ()
138174 .withSession ( session -> session .find ( SimpleEntity .class , 2L ) ) )
139175 .invoke ( entity -> assertThat ( entity )
@@ -142,13 +178,19 @@ public void testRelatedUpdateRelatedDifferentTransaction(VertxTestContext contex
142178 );
143179 }
144180
145- private static void shouldUsePrimaryKey () {
181+ private static void shouldUsePrimaryKeyForUpdate () {
146182 assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
147183 assertThat ( sqlTracker .getLoggedQueries ().get ( 0 ) )
148184 .matches ( "update SimpleEntity set status=.+ where primary_key=.+" );
149185 }
150186
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 () {
152194 // Not all databases have a rowId column
153195 String rowId = getDialect ().rowId ( "" );
154196 String column = rowId == null ? "primary_key" : rowId ;
@@ -157,6 +199,15 @@ private static void shouldUseRowId() {
157199 .matches ( "update SimpleEntity set status=.+ where " + column + "=.+" );
158200 }
159201
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+
160211 @ Entity (name = "SimpleEntity" )
161212 @ Table (name = "SimpleEntity" )
162213 @ RowId
0 commit comments