From 07477f3323b3ffbca1017770fe42b6b5ec9c2ea4 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 19 Dec 2024 13:07:00 +0100 Subject: [PATCH] #2042 add insertMultiple() and friends, with semantics aligned with ORM 7 see https://github.com/hibernate/hibernate-reactive/issues/2042 --- .../org/hibernate/reactive/mutiny/Mutiny.java | 48 +++++++++++++++++-- .../impl/MutinyStatelessSessionImpl.java | 21 ++++++++ .../org/hibernate/reactive/stage/Stage.java | 40 ++++++++++++++++ .../stage/impl/StageStatelessSessionImpl.java | 21 ++++++++ 4 files changed, 126 insertions(+), 4 deletions(-) diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java index 493c0ed03..e0a0eb4e4 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java @@ -1730,6 +1730,16 @@ default Uni get(Class entityClass, Object id, LockModeType lockModeTyp */ Uni insertAll(int batchSize, Object... entities); + /** + * Insert multiple rows, using the size of the + * given list as the batch size. + * + * @param entities new transient instances + * + * @see org.hibernate.StatelessSession#insert(Object) + */ + Uni insertMultiple(List entities); + /** * Delete a row. * @@ -1758,6 +1768,16 @@ default Uni get(Class entityClass, Object id, LockModeType lockModeTyp */ Uni deleteAll(int batchSize, Object... entities); + /** + * Delete multiple rows, using the size of the + * given list as the batch size. + * + * @param entities detached entity instances + * + * @see org.hibernate.StatelessSession#delete(Object) + */ + Uni deleteMultiple(List entities); + /** * Update a row. * @@ -1787,13 +1807,14 @@ default Uni get(Class entityClass, Object id, LockModeType lockModeTyp Uni updateAll(int batchSize, Object... entities); /** - * Refresh the entity instance state from the database. + * Update multiple rows, using the size of the + * given list as the batch size. * - * @param entity The entity to be refreshed. + * @param entities detached entity instances * - * @see org.hibernate.StatelessSession#refresh(Object) + * @see org.hibernate.StatelessSession#update(Object) */ - Uni refresh(Object entity); + Uni updateMultiple(List entities); /** * Use a SQL {@code merge into} statement to perform an upsert. @@ -1817,6 +1838,15 @@ default Uni get(Class entityClass, Object id, LockModeType lockModeTyp @Incubating Uni upsert(String entityName, Object entity); + /** + * Refresh the entity instance state from the database. + * + * @param entity The entity to be refreshed. + * + * @see org.hibernate.StatelessSession#refresh(Object) + */ + Uni refresh(Object entity); + /** * Refresh the entity instance state from the database. * @@ -1837,6 +1867,16 @@ default Uni get(Class entityClass, Object id, LockModeType lockModeTyp */ Uni refreshAll(int batchSize, Object... entities); + /** + * Refresh the entity instance state from the database + * using the size of the given list as the batch size. + * + * @param entities The entities to be refreshed. + * + * @see org.hibernate.StatelessSession#refresh(Object) + */ + Uni refreshMultiple(List entities); + /** * Refresh the entity instance state from the database. * diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinyStatelessSessionImpl.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinyStatelessSessionImpl.java index 275cd1cef..ba1bee23e 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinyStatelessSessionImpl.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinyStatelessSessionImpl.java @@ -17,6 +17,7 @@ import org.hibernate.reactive.pool.ReactiveConnection; import org.hibernate.reactive.session.ReactiveStatelessSession; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.function.Function; @@ -128,6 +129,11 @@ public Uni insertAll(int batchSize, Object... entities) { return uni( () -> delegate.reactiveInsertAll( batchSize, entities ) ); } + @Override + public Uni insertMultiple(List entities) { + return insertAll( entities.size(), entities.toArray() ); + } + @Override public Uni delete(Object entity) { return uni( () -> delegate.reactiveDelete( entity ) ); @@ -143,6 +149,11 @@ public Uni deleteAll(int batchSize, Object... entities) { return uni( () -> delegate.reactiveDeleteAll( entities ) ); } + @Override + public Uni deleteMultiple(List entities) { + return deleteAll( entities.size(), entities.toArray() ); + } + @Override public Uni update(Object entity) { return uni( () -> delegate.reactiveUpdate( entity ) ); @@ -158,6 +169,11 @@ public Uni updateAll(int batchSize, Object... entities) { return uni( () -> delegate.reactiveUpdateAll( batchSize, entities ) ); } + @Override + public Uni updateMultiple(List entities) { + return updateAll( entities.size(), entities.toArray() ); + } + @Override public Uni refresh(Object entity) { return uni( () -> delegate.reactiveRefresh( entity ) ); @@ -183,6 +199,11 @@ public Uni refreshAll(int batchSize, Object... entities) { return uni( () -> delegate.reactiveRefreshAll( batchSize, entities ) ); } + @Override + public Uni refreshMultiple(List entities) { + return refreshAll( entities.size(), entities.toArray() ); + } + @Override public Uni refresh(Object entity, LockMode lockMode) { return uni( () -> delegate.reactiveRefresh( entity, lockMode ) ); diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java index f84034da5..f009b8dca 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java @@ -1775,6 +1775,16 @@ default CompletionStage get(Class entityClass, Object id, LockModeType */ CompletionStage insert(int batchSize, Object... entities); + /** + * Insert multiple rows, using the size of the + * given list as the batch size. + * + * @param entities new transient instances + * + * @see org.hibernate.StatelessSession#insert(Object) + */ + CompletionStage insertMultiple(List entities); + /** * Delete a row. * @@ -1803,6 +1813,16 @@ default CompletionStage get(Class entityClass, Object id, LockModeType */ CompletionStage delete(int batchSize, Object... entities); + /** + * Delete multiple rows, using the size of the + * given list as the batch size. + * + * @param entities detached entity instances + * + * @see org.hibernate.StatelessSession#delete(Object) + */ + CompletionStage deleteMultiple(List entities); + /** * Update a row. * @@ -1831,6 +1851,16 @@ default CompletionStage get(Class entityClass, Object id, LockModeType */ CompletionStage update(int batchSize, Object... entities); + /** + * Update multiple rows, using the size of the + * given list as the batch size. + * + * @param entities a detached entity instance + * + * @see org.hibernate.StatelessSession#update(Object) + */ + CompletionStage updateMultiple(List entities); + /** * Refresh the entity instance state from the database. * @@ -1859,6 +1889,16 @@ default CompletionStage get(Class entityClass, Object id, LockModeType */ CompletionStage refresh(int batchSize, Object... entities); + /** + * Refresh the entity instance state from the database, + * using the size of the given list as the batch size. + * + * @param entities The entities to be refreshed. + * + * @see org.hibernate.StatelessSession#refresh(Object) + */ + CompletionStage refreshMultiple(List entities); + /** * Refresh the entity instance state from the database. * diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageStatelessSessionImpl.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageStatelessSessionImpl.java index 48e7d8e9a..6e05c747c 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageStatelessSessionImpl.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageStatelessSessionImpl.java @@ -19,6 +19,7 @@ import org.hibernate.reactive.stage.Stage.Query; import org.hibernate.reactive.stage.Stage.SelectionQuery; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.function.Function; @@ -67,6 +68,11 @@ public CompletionStage insert(int batchSize, Object... entities) { return delegate.reactiveInsertAll( batchSize, entities ); } + @Override + public CompletionStage insertMultiple(List entities) { + return delegate.reactiveInsertAll( entities.size(), entities.toArray() ); + } + @Override public CompletionStage delete(Object entity) { return delegate.reactiveDelete( entity ); @@ -82,6 +88,11 @@ public CompletionStage delete(int batchSize, Object... entities) { return delegate.reactiveDeleteAll( batchSize, entities ); } + @Override + public CompletionStage deleteMultiple(List entities) { + return delegate.reactiveDeleteAll( entities.size(), entities.toArray() ); + } + @Override public CompletionStage update(Object entity) { return delegate.reactiveUpdate( entity ); @@ -97,6 +108,11 @@ public CompletionStage update(int batchSize, Object... entities) { return delegate.reactiveUpdateAll( batchSize, entities ); } + @Override + public CompletionStage updateMultiple(List entities) { + return delegate.reactiveUpdateAll( entities.size(), entities.toArray() ); + } + @Override public CompletionStage refresh(Object entity) { return delegate.reactiveRefresh( entity ); @@ -112,6 +128,11 @@ public CompletionStage refresh(int batchSize, Object... entities) { return delegate.reactiveRefreshAll( batchSize, entities ); } + @Override + public CompletionStage refreshMultiple(List entities) { + return delegate.reactiveRefreshAll( entities.size(), entities.toArray() ); + } + @Override public CompletionStage refresh(Object entity, LockMode lockMode) { return delegate.reactiveRefresh( entity, lockMode );