Skip to content

Commit 679b8ba

Browse files
committed
SelectionSpecification fix
1 parent 4906b17 commit 679b8ba

File tree

8 files changed

+420
-444
lines changed

8 files changed

+420
-444
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,22 @@ default <T> Uni<T> get(Class<T> entityClass, Object id, LockModeType lockModeTyp
16331633
@Deprecated
16341634
<R> Query<R> createQuery(String queryString);
16351635

1636+
/**
1637+
* Create a typed {@link Query} instance for the given typed query reference.
1638+
*
1639+
* @param typedQueryReference the type query reference
1640+
*
1641+
* @return The {@link Query} instance for execution
1642+
*
1643+
* @throws IllegalArgumentException if a query has not been
1644+
* defined with the name of the typed query reference or if
1645+
* the query result is found to not be assignable to
1646+
* result class of the typed query reference
1647+
*
1648+
* @see org.hibernate.query.QueryProducer#createQuery(TypedQueryReference)
1649+
*/
1650+
<R> Query<R> createQuery(TypedQueryReference<R> typedQueryReference);
1651+
16361652
/**
16371653
* Create an instance of {@link SelectionQuery} for the given HQL/JPQL
16381654
* query string and query result type.

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

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import io.smallrye.mutiny.Uni;
99
import jakarta.persistence.EntityGraph;
10+
import jakarta.persistence.TypedQueryReference;
1011
import jakarta.persistence.criteria.CriteriaDelete;
1112
import jakarta.persistence.criteria.CriteriaQuery;
1213
import jakarta.persistence.criteria.CriteriaUpdate;
@@ -17,6 +18,7 @@
1718
import org.hibernate.reactive.mutiny.Mutiny.Query;
1819
import org.hibernate.reactive.mutiny.Mutiny.SelectionQuery;
1920
import org.hibernate.reactive.pool.ReactiveConnection;
21+
import org.hibernate.reactive.query.ReactiveQuery;
2022
import org.hibernate.reactive.session.ReactiveStatelessSession;
2123

2224
import java.util.List;
@@ -71,6 +73,12 @@ public <T> Uni<T> get(EntityGraph<T> entityGraph, Object id) {
7173
return uni( () -> delegate.reactiveGet( entityClass, id, null, entityGraph ) );
7274
}
7375

76+
@Override
77+
public <R> Query<R> createQuery(TypedQueryReference<R> typedQueryReference) {
78+
ReactiveQuery<R> reactiveQuery = delegate.createReactiveQuery( typedQueryReference );
79+
return new MutinyQueryImpl<>( reactiveQuery, factory );
80+
}
81+
7482
@Override
7583
public <R> Query<R> createQuery(String queryString) {
7684
return new MutinyQueryImpl<>( delegate.createReactiveQuery( queryString ), factory );

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/impl/ReactiveSessionImpl.java

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.hibernate.reactive.session.impl;
77

88
import jakarta.persistence.TypedQueryReference;
9+
import jakarta.persistence.criteria.CommonAbstractCriteria;
910
import java.lang.invoke.MethodHandles;
1011
import java.util.List;
1112
import java.util.Map;
@@ -77,6 +78,8 @@
7778
import org.hibernate.query.criteria.JpaCriteriaInsert;
7879
import org.hibernate.query.hql.spi.SqmQueryImplementor;
7980
import org.hibernate.query.named.NamedResultSetMappingMemento;
81+
import org.hibernate.query.specification.internal.MutationSpecificationImpl;
82+
import org.hibernate.query.specification.internal.SelectionSpecificationImpl;
8083
import org.hibernate.query.spi.HqlInterpretation;
8184
import org.hibernate.query.spi.QueryImplementor;
8285
import org.hibernate.query.sql.spi.NamedNativeQueryMemento;
@@ -361,6 +364,14 @@ protected <T> ReactiveQueryImplementor<T> createReactiveCriteriaQuery(SqmStateme
361364
@Override
362365
public <R> ReactiveQuery<R> createReactiveQuery(TypedQueryReference<R> typedQueryReference) {
363366
checksBeforeQueryCreation();
367+
if ( typedQueryReference instanceof SelectionSpecificationImpl<R> specification ) {
368+
final CriteriaQuery<R> query = specification.buildCriteria( getCriteriaBuilder() );
369+
return new ReactiveQuerySqmImpl<>( (SqmStatement<R>) query, specification.getResultType(), this );
370+
}
371+
if ( typedQueryReference instanceof MutationSpecificationImpl<?> specification ) {
372+
final CommonAbstractCriteria query = specification.buildCriteria( getCriteriaBuilder() );
373+
return new ReactiveQuerySqmImpl<>( (SqmStatement<R>) query, (Class<R>) specification.getResultType(), this );
374+
}
364375
@SuppressWarnings("unchecked")
365376
// this cast is fine because of all our impls of TypedQueryReference return Class<R>
366377
final Class<R> resultType = (Class<R>) typedQueryReference.getResultType();

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/impl/ReactiveStatelessSessionImpl.java

+20-16
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
*/
66
package org.hibernate.reactive.session.impl;
77

8-
import jakarta.persistence.TypedQueryReference;
9-
import java.util.ArrayList;
10-
import java.util.List;
11-
import java.util.concurrent.CompletableFuture;
12-
import java.util.concurrent.CompletionStage;
13-
import java.util.function.Supplier;
14-
158
import org.hibernate.HibernateException;
169
import org.hibernate.LockMode;
1710
import org.hibernate.LockOptions;
@@ -49,6 +42,8 @@
4942
import org.hibernate.query.criteria.JpaCriteriaInsert;
5043
import org.hibernate.query.hql.spi.SqmQueryImplementor;
5144
import org.hibernate.query.named.NamedResultSetMappingMemento;
45+
import org.hibernate.query.specification.internal.MutationSpecificationImpl;
46+
import org.hibernate.query.specification.internal.SelectionSpecificationImpl;
5247
import org.hibernate.query.spi.HqlInterpretation;
5348
import org.hibernate.query.spi.QueryImplementor;
5449
import org.hibernate.query.sql.spi.NativeQueryImplementor;
@@ -84,6 +79,8 @@
8479

8580
import jakarta.persistence.EntityGraph;
8681
import jakarta.persistence.Tuple;
82+
import jakarta.persistence.TypedQueryReference;
83+
import jakarta.persistence.criteria.CommonAbstractCriteria;
8784
import jakarta.persistence.criteria.CriteriaDelete;
8885
import jakarta.persistence.criteria.CriteriaQuery;
8986
import jakarta.persistence.criteria.CriteriaUpdate;
@@ -225,15 +222,14 @@ public <T> CompletionStage<List<T>> reactiveGet(Class<T> entityClass, Object...
225222
Object[] sids = new Object[ids.length];
226223
System.arraycopy( ids, 0, sids, 0, ids.length );
227224

228-
final CompletionStage<? extends List<?>> stage =
229-
getEntityPersister( entityClass.getName() )
230-
.reactiveMultiLoad( sids, this, StatelessSessionImpl.MULTI_ID_LOAD_OPTIONS )
231-
.whenComplete( (v, e) -> {
232-
if ( getPersistenceContext().isLoadFinished() ) {
233-
getPersistenceContext().clear();
234-
}
235-
} );
236-
return (CompletionStage<List<T>>) stage;
225+
return getEntityPersister( entityClass.getName() )
226+
.reactiveMultiLoad( sids, this, StatelessSessionImpl.MULTI_ID_LOAD_OPTIONS )
227+
.whenComplete( (v, e) -> {
228+
if ( getPersistenceContext().isLoadFinished() ) {
229+
getPersistenceContext().clear();
230+
}
231+
} )
232+
.thenApply( objects -> (List<T>) objects );
237233
}
238234

239235
@Override
@@ -803,6 +799,14 @@ public <T> RootGraphImplementor<T> getEntityGraph(Class<T> entity, String name)
803799
@Override
804800
public <R> ReactiveQuery<R> createReactiveQuery(TypedQueryReference<R> typedQueryReference) {
805801
checksBeforeQueryCreation();
802+
if ( typedQueryReference instanceof SelectionSpecificationImpl<R> specification ) {
803+
final CriteriaQuery<R> query = specification.buildCriteria( getCriteriaBuilder() );
804+
return new ReactiveQuerySqmImpl<>( (SqmStatement<R>) query, specification.getResultType(), this );
805+
}
806+
if ( typedQueryReference instanceof MutationSpecificationImpl<?> specification ) {
807+
final CommonAbstractCriteria query = specification.buildCriteria( getCriteriaBuilder() );
808+
return new ReactiveQuerySqmImpl<>( (SqmStatement<R>) query, (Class<R>) specification.getResultType(), this );
809+
}
806810
@SuppressWarnings("unchecked")
807811
// this cast is fine because of all our impls of TypedQueryReference return Class<R>
808812
final Class<R> resultType = (Class<R>) typedQueryReference.getResultType();

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -988,11 +988,11 @@ default CompletionStage<Void> lock(Object entity, LockModeType lockModeType) {
988988
MutationQuery createMutationQuery(JpaCriteriaInsert<?> insert);
989989

990990
/**
991-
* Create a typed {@link org.hibernate.query.Query} instance for the given typed query reference.
991+
* Create a typed {@link Query} instance for the given typed query reference.
992992
*
993993
* @param typedQueryReference the type query reference
994994
*
995-
* @return The {@link org.hibernate.query.Query} instance for execution
995+
* @return The {@link Query} instance for execution
996996
*
997997
* @throws IllegalArgumentException if a query has not been
998998
* defined with the name of the typed query reference or if
@@ -1655,6 +1655,22 @@ default <T> CompletionStage<T> get(Class<T> entityClass, Object id, LockModeType
16551655
@Deprecated
16561656
<R> Query<R> createQuery(String queryString);
16571657

1658+
/**
1659+
* Create a typed {@link Query} instance for the given typed query reference.
1660+
*
1661+
* @param typedQueryReference the type query reference
1662+
*
1663+
* @return The {@link Query} instance for execution
1664+
*
1665+
* @throws IllegalArgumentException if a query has not been
1666+
* defined with the name of the typed query reference or if
1667+
* the query result is found to not be assignable to
1668+
* result class of the typed query reference
1669+
*
1670+
* @see org.hibernate.query.QueryProducer#createQuery(TypedQueryReference)
1671+
*/
1672+
<R> Query<R> createQuery(TypedQueryReference<R> typedQueryReference);
1673+
16581674
/**
16591675
* Create an instance of {@link SelectionQuery} for the given HQL/JPQL
16601676
* query string and query result type.

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

+8
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
import org.hibernate.query.criteria.JpaCriteriaInsert;
1111
import org.hibernate.reactive.common.ResultSetMapping;
1212
import org.hibernate.reactive.pool.ReactiveConnection;
13+
import org.hibernate.reactive.query.ReactiveQuery;
1314
import org.hibernate.reactive.session.ReactiveStatelessSession;
1415
import org.hibernate.reactive.stage.Stage;
1516
import org.hibernate.reactive.stage.Stage.MutationQuery;
1617
import org.hibernate.reactive.stage.Stage.Query;
1718
import org.hibernate.reactive.stage.Stage.SelectionQuery;
1819

1920
import jakarta.persistence.EntityGraph;
21+
import jakarta.persistence.TypedQueryReference;
2022
import jakarta.persistence.criteria.CriteriaDelete;
2123
import jakarta.persistence.criteria.CriteriaQuery;
2224
import jakarta.persistence.criteria.CriteriaUpdate;
@@ -289,6 +291,12 @@ public <R> Query<R> createQuery(String queryString) {
289291
return new StageQueryImpl<>( delegate.createReactiveQuery( queryString ) );
290292
}
291293

294+
@Override
295+
public <R> Query<R> createQuery(TypedQueryReference<R> typedQueryReference) {
296+
ReactiveQuery<R> reactiveQuery = delegate.createReactiveQuery( typedQueryReference );
297+
return new StageQueryImpl<>( reactiveQuery );
298+
}
299+
292300
@Override
293301
public <R> SelectionQuery<R> createSelectionQuery(String queryString, Class<R> resultType) {
294302
return new StageSelectionQueryImpl<>( delegate.createReactiveSelectionQuery( queryString, resultType ) );

0 commit comments

Comments
 (0)