Skip to content

Commit b2c0ca8

Browse files
committed
Initial test cases
1 parent 57b7615 commit b2c0ca8

File tree

7 files changed

+132
-19
lines changed

7 files changed

+132
-19
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java

+11
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ interface SelectionQuery<R> extends AbstractQuery {
194194
*/
195195
Uni<R> getSingleResultOrNull();
196196

197+
/**
198+
* Determine the size of the query result list that would be
199+
* returned by calling {@link #getResultList()} with no
200+
* {@linkplain #getFirstResult() offset} or
201+
* {@linkplain #getMaxResults() limit} applied to the query.
202+
*
203+
* @return the size of the list that would be returned
204+
*/
205+
@Incubating
206+
Uni<Long> getResultCount();
207+
197208
/**
198209
* Asynchronously execute this query, returning the query results
199210
* as a {@link List}, via a {@link Uni}. If the query

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinyQueryImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public int getMaxResults() {
4747
return delegate.getMaxResults();
4848
}
4949

50+
@Override
51+
public Uni<Long> getResultCount() {
52+
throw new UnsupportedOperationException();
53+
}
54+
5055
@Override
5156
public Uni<List<R>> getResultList() {
5257
return uni( delegate::getReactiveResultList );

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinySelectionQueryImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public int getMaxResults() {
4545
return delegate.getMaxResults();
4646
}
4747

48+
@Override
49+
public Uni<Long> getResultCount() {
50+
throw new UnsupportedOperationException();
51+
}
52+
4853
@Override
4954
public Uni<List<R>> getResultList() {
5055
return uni( delegate::getReactiveResultList );

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java

+37-19
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
*/
66
package org.hibernate.reactive.stage;
77

8-
import jakarta.persistence.CacheRetrieveMode;
9-
import jakarta.persistence.CacheStoreMode;
10-
import jakarta.persistence.EntityGraph;
11-
import jakarta.persistence.FlushModeType;
12-
import jakarta.persistence.LockModeType;
13-
import jakarta.persistence.Parameter;
14-
import jakarta.persistence.criteria.CriteriaBuilder;
15-
import jakarta.persistence.criteria.CriteriaDelete;
16-
import jakarta.persistence.criteria.CriteriaQuery;
17-
import jakarta.persistence.criteria.CriteriaUpdate;
18-
import jakarta.persistence.metamodel.Attribute;
19-
import jakarta.persistence.metamodel.Metamodel;
8+
import java.lang.invoke.MethodHandles;
9+
import java.util.List;
10+
import java.util.concurrent.CompletionStage;
11+
import java.util.function.BiFunction;
12+
import java.util.function.Function;
13+
2014
import org.hibernate.Cache;
21-
import org.hibernate.*;
15+
import org.hibernate.CacheMode;
16+
import org.hibernate.Filter;
17+
import org.hibernate.FlushMode;
18+
import org.hibernate.Incubating;
19+
import org.hibernate.LockMode;
2220
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
2321
import org.hibernate.collection.spi.AbstractPersistentCollection;
2422
import org.hibernate.collection.spi.PersistentCollection;
@@ -38,16 +36,25 @@
3836
import org.hibernate.reactive.util.impl.CompletionStages;
3937
import org.hibernate.stat.Statistics;
4038

41-
import java.lang.invoke.MethodHandles;
42-
import java.util.List;
43-
import java.util.concurrent.CompletionStage;
44-
import java.util.function.BiFunction;
45-
import java.util.function.Function;
39+
import jakarta.persistence.CacheRetrieveMode;
40+
import jakarta.persistence.CacheStoreMode;
41+
import jakarta.persistence.EntityGraph;
42+
import jakarta.persistence.FlushModeType;
43+
import jakarta.persistence.LockModeType;
44+
import jakarta.persistence.Parameter;
45+
import jakarta.persistence.criteria.CriteriaBuilder;
46+
import jakarta.persistence.criteria.CriteriaDelete;
47+
import jakarta.persistence.criteria.CriteriaQuery;
48+
import jakarta.persistence.criteria.CriteriaUpdate;
49+
import jakarta.persistence.metamodel.Attribute;
50+
import jakarta.persistence.metamodel.Metamodel;
4651

4752
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
4853
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
4954
import static org.hibernate.internal.util.LockModeConverter.convertToLockMode;
50-
import static org.hibernate.jpa.internal.util.CacheModeHelper.*;
55+
import static org.hibernate.jpa.internal.util.CacheModeHelper.interpretCacheMode;
56+
import static org.hibernate.jpa.internal.util.CacheModeHelper.interpretCacheRetrieveMode;
57+
import static org.hibernate.jpa.internal.util.CacheModeHelper.interpretCacheStoreMode;
5158

5259
/**
5360
* An API for Hibernate Reactive where non-blocking operations are
@@ -188,6 +195,17 @@ interface SelectionQuery<R> extends AbstractQuery {
188195
*/
189196
CompletionStage<R> getSingleResultOrNull();
190197

198+
/**
199+
* Determine the size of the query result list that would be
200+
* returned by calling {@link #getResultList()} with no
201+
* {@linkplain #getFirstResult() offset} or
202+
* {@linkplain #getMaxResults() limit} applied to the query.
203+
*
204+
* @return the size of the list that would be returned
205+
*/
206+
@Incubating
207+
CompletionStage<Long> getResultCount();
208+
191209
/**
192210
* Asynchronously execute this query, returning the query results
193211
* as a {@link List}, via a {@link CompletionStage}. If the query

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageQueryImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public int getMaxResults() {
3838
return delegate.getMaxResults();
3939
}
4040

41+
@Override
42+
public CompletionStage<Long> getResultCount() {
43+
throw new UnsupportedOperationException();
44+
}
45+
4146
@Override
4247
public CompletionStage<List<R>> getResultList() {
4348
return delegate.getReactiveResultList();

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageSelectionQueryImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public int getMaxResults() {
4141
return delegate.getMaxResults();
4242
}
4343

44+
@Override
45+
public CompletionStage<Long> getResultCount() {
46+
throw new UnsupportedOperationException();
47+
}
48+
4449
@Override
4550
public CompletionStage<List<T>> getResultList() {
4651
return delegate.getReactiveResultList();

hibernate-reactive-core/src/test/java/org/hibernate/reactive/QueryTest.java

+64
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,70 @@ public void testSingleResultOrNullNonUniqueException(VertxTestContext context) {
606606
);
607607
}
608608

609+
@Test
610+
public void testSelectionQueryGetResultCountWithStage(VertxTestContext context) {
611+
Author author1 = new Author( "Iain M. Banks" );
612+
Author author2 = new Author( "Neal Stephenson" );
613+
test( context, getSessionFactory()
614+
.withTransaction( s -> s.persist( author1, author2 ) )
615+
.thenCompose( v -> getSessionFactory().withSession( s -> s
616+
.createSelectionQuery( "from Author", Author.class )
617+
.getResultCount() ) )
618+
.thenAccept( count -> assertEquals( 2L, count ) )
619+
);
620+
}
621+
622+
@Test
623+
public void testQueryGetResultCountWithStage(VertxTestContext context) {
624+
Author author1 = new Author( "Iain M. Banks" );
625+
Author author2 = new Author( "Neal Stephenson" );
626+
test( context, getSessionFactory()
627+
.withTransaction( s -> s.persist( author1, author2 ) )
628+
.thenCompose( v -> getSessionFactory().withSession( s -> s
629+
.createQuery( "from Author", Author.class )
630+
.getResultCount() ) )
631+
.thenAccept( count -> assertEquals( 2L, count ) )
632+
.thenCompose( v -> getSessionFactory().withSession( s -> s
633+
.createQuery( "from Author", Author.class )
634+
.setMaxResults( 1 )
635+
.setFirstResult( 1 )
636+
.getResultCount() ) )
637+
.thenAccept( count -> assertEquals( 2L, count ) )
638+
);
639+
}
640+
641+
@Test
642+
public void testSelectionQueryGetResultCountWithMutiny(VertxTestContext context) {
643+
Author author1 = new Author( "Iain M. Banks" );
644+
Author author2 = new Author( "Neal Stephenson" );
645+
test( context, getSessionFactory()
646+
.withTransaction( s -> s.persist( author1, author2 ) )
647+
.thenCompose( v -> getSessionFactory().withSession( s -> s
648+
.createSelectionQuery( "from Author", Author.class )
649+
.getResultCount() ) )
650+
.thenAccept( count -> assertEquals( 2L, count ) )
651+
);
652+
}
653+
654+
@Test
655+
public void testQueryGetResultCountWithMutiny(VertxTestContext context) {
656+
Author author1 = new Author( "Iain M. Banks" );
657+
Author author2 = new Author( "Neal Stephenson" );
658+
test( context, getMutinySessionFactory()
659+
.withTransaction( s -> s.persistAll( author1, author2 ) )
660+
.chain( () -> getMutinySessionFactory().withSession( s -> s
661+
.createQuery( "from Author", Author.class )
662+
.getResultCount() ) )
663+
.invoke( count -> assertEquals( 2L, count ) )
664+
.chain( () -> getMutinySessionFactory().withSession( s -> s
665+
.createQuery( "from Author", Author.class )
666+
.setMaxResults( 1 )
667+
.setFirstResult( 1 )
668+
.getResultCount() ) )
669+
.invoke( count -> assertEquals( 2L, count ) )
670+
);
671+
}
672+
609673
@NamedNativeQuery(
610674
name = SQL_NAMED_QUERY,
611675
resultClass = Object[].class,

0 commit comments

Comments
 (0)