HHH-15030 - SelectQuery, MutationQuery, etc

SelectQuery as typed
This commit is contained in:
Steve Ebersole 2022-01-21 23:48:58 -06:00
parent dccb1580fe
commit 6f2273d749
12 changed files with 372 additions and 302 deletions

View File

@ -495,10 +495,15 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
} }
@Override @Override
public SelectionQuery createSelectQuery(String hqlString) { public SelectionQuery<?> createSelectQuery(String hqlString) {
return queryDelegate().createSelectQuery( hqlString ); return queryDelegate().createSelectQuery( hqlString );
} }
@Override
public <R> SelectionQuery<R> createSelectQuery(String hqlString, Class<R> resultType) {
return queryDelegate().createSelectQuery( hqlString, resultType );
}
@Override @Override
public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultType) { public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultType) {
return queryDelegate().createQuery( queryString, resultType ); return queryDelegate().createQuery( queryString, resultType );

View File

@ -11,6 +11,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.UUID; import java.util.UUID;
import java.util.function.Function; import java.util.function.Function;
@ -56,6 +57,7 @@ import org.hibernate.query.IllegalNamedQueryOptionsException;
import org.hibernate.query.IllegalSelectQueryException; import org.hibernate.query.IllegalSelectQueryException;
import org.hibernate.query.MutationQuery; import org.hibernate.query.MutationQuery;
import org.hibernate.query.Query; import org.hibernate.query.Query;
import org.hibernate.query.QueryTypeMismatchException;
import org.hibernate.query.SelectionQuery; import org.hibernate.query.SelectionQuery;
import org.hibernate.query.UnknownNamedQueryException; import org.hibernate.query.UnknownNamedQueryException;
import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.hibernate.query.criteria.HibernateCriteriaBuilder;
@ -71,7 +73,7 @@ import org.hibernate.query.sql.spi.NamedNativeQueryMemento;
import org.hibernate.query.sql.spi.NativeQueryImplementor; import org.hibernate.query.sql.spi.NativeQueryImplementor;
import org.hibernate.query.sqm.SqmSelectionQuery; import org.hibernate.query.sqm.SqmSelectionQuery;
import org.hibernate.query.sqm.internal.QuerySqmImpl; import org.hibernate.query.sqm.internal.QuerySqmImpl;
import org.hibernate.query.sqm.internal.SqmSelectQueryImpl; import org.hibernate.query.sqm.internal.SqmSelectionQueryImpl;
import org.hibernate.query.sqm.tree.SqmDmlStatement; import org.hibernate.query.sqm.tree.SqmDmlStatement;
import org.hibernate.query.sqm.tree.SqmStatement; import org.hibernate.query.sqm.tree.SqmStatement;
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement; import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
@ -665,7 +667,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
@Override @Override
public SelectionQuery createSelectQuery(String hqlString) { public SelectionQuery<?> createSelectQuery(String hqlString) {
checkOpen(); checkOpen();
pulseTransactionCoordinator(); pulseTransactionCoordinator();
delayedAfterCompletion(); delayedAfterCompletion();
@ -682,7 +684,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
throw new IllegalSelectQueryException( "Expecting a selection query, but found `" + hqlString + "`" ); throw new IllegalSelectQueryException( "Expecting a selection query, but found `" + hqlString + "`" );
} }
final SqmSelectionQuery query = new SqmSelectQueryImpl( hqlString, hqlInterpretation, this ); final SqmSelectionQuery<?> query = new SqmSelectionQueryImpl<>( hqlString, hqlInterpretation, this );
query.setComment( hqlString ); query.setComment( hqlString );
applyQuerySettingsAndHints( query ); applyQuerySettingsAndHints( query );
@ -695,6 +697,27 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
} }
@Override
public <R> SelectionQuery<R> createSelectQuery(String hqlString, Class<R> expectedResultType) {
final SelectionQuery<?> selectQuery = createSelectQuery( hqlString );
//noinspection unchecked
final Class<?> resultType = ( (SqmSelectionQueryImpl<R>) selectQuery ).getResultType();
if ( resultType == null || expectedResultType.isAssignableFrom( resultType ) ) {
//noinspection unchecked
return (SelectionQuery<R>) selectQuery;
}
throw new QueryTypeMismatchException(
String.format(
Locale.ROOT,
"Query result-type error - expecting `%s`, but found `%s`",
expectedResultType.getName(),
resultType.getName()
)
);
}
@Override @Override
public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultClass) { public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultClass) {
checkOpen(); checkOpen();

View File

@ -66,7 +66,7 @@ import jakarta.persistence.TypedQuery;
* @param <R> The result type, for typed queries, or {@link Object} for untyped queries * @param <R> The result type, for typed queries, or {@link Object} for untyped queries
*/ */
@Incubating @Incubating
public interface Query<R> extends SelectionQuery, MutationQuery, TypedQuery<R> { public interface Query<R> extends SelectionQuery<R>, MutationQuery, TypedQuery<R> {
/** /**
* Execute the query and return the query results as a {@link List}. * Execute the query and return the query results as a {@link List}.

View File

@ -168,7 +168,19 @@ public interface QueryProducer {
* @throws IllegalSelectQueryException if the given HQL query * @throws IllegalSelectQueryException if the given HQL query
* is an insert, update or delete query * is an insert, update or delete query
*/ */
SelectionQuery createSelectQuery(String hqlString); SelectionQuery<?> createSelectQuery(String hqlString);
/**
* Create a {@link SelectionQuery} reference for the given HQL.
*
* Only valid for select queries
*
* @see jakarta.persistence.EntityManager#createQuery(String)
*
* @throws IllegalSelectQueryException if the given HQL query
* is an insert, update or delete query
*/
<R> SelectionQuery<R> createSelectQuery(String hqlString, Class<R> resultType);
/** /**
* Create a MutationQuery reference for the given HQL insert, * Create a MutationQuery reference for the given HQL insert,

View File

@ -39,7 +39,7 @@ import jakarta.persistence.TemporalType;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@Incubating @Incubating
public interface SelectionQuery extends CommonQueryContract { public interface SelectionQuery<R> extends CommonQueryContract {
/** /**
* Execute the query and return the query results as a {@link List}. * Execute the query and return the query results as a {@link List}.
* If the query contains multiple items in the selection list, then * If the query contains multiple items in the selection list, then
@ -48,7 +48,7 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @return the result list * @return the result list
*/ */
List<?> list(); List<R> list();
/** /**
* Execute the query and return the query results as a {@link List}. * Execute the query and return the query results as a {@link List}.
@ -58,7 +58,7 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @return the results as a list * @return the results as a list
*/ */
default List<?> getResultList() { default List<R> getResultList() {
return list(); return list();
} }
@ -70,7 +70,7 @@ public interface SelectionQuery extends CommonQueryContract {
* @apiNote The exact behavior of this method depends somewhat * @apiNote The exact behavior of this method depends somewhat
* on the JDBC driver's {@link java.sql.ResultSet} scrolling support * on the JDBC driver's {@link java.sql.ResultSet} scrolling support
*/ */
ScrollableResults<?> scroll(); ScrollableResults<R> scroll();
/** /**
* Returns scrollable access to the query results. The capabilities of the * Returns scrollable access to the query results. The capabilities of the
@ -79,7 +79,7 @@ public interface SelectionQuery extends CommonQueryContract {
* @apiNote The exact behavior of this method depends somewhat * @apiNote The exact behavior of this method depends somewhat
* on the JDBC driver's {@link java.sql.ResultSet} scrolling support * on the JDBC driver's {@link java.sql.ResultSet} scrolling support
*/ */
ScrollableResults<?> scroll(ScrollMode scrollMode); ScrollableResults<R> scroll(ScrollMode scrollMode);
/** /**
* Execute the query and return the query results as a {@link Stream}. * Execute the query and return the query results as a {@link Stream}.
@ -92,7 +92,7 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @return The results as a {@link Stream} * @return The results as a {@link Stream}
*/ */
default Stream<?> getResultStream() { default Stream<R> getResultStream() {
return stream(); return stream();
} }
@ -109,7 +109,7 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @since 5.2 * @since 5.2
*/ */
default Stream<?> stream() { default Stream<R> stream() {
return getResultStream(); return getResultStream();
} }
@ -121,7 +121,7 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @throws NonUniqueResultException if there is more than one matching result * @throws NonUniqueResultException if there is more than one matching result
*/ */
Object uniqueResult(); R uniqueResult();
/** /**
* Execute the query and return the single result of the query, * Execute the query and return the single result of the query,
@ -132,7 +132,7 @@ public interface SelectionQuery extends CommonQueryContract {
* @throws jakarta.persistence.NonUniqueResultException if there is more than one matching result * @throws jakarta.persistence.NonUniqueResultException if there is more than one matching result
* @throws jakarta.persistence.NoResultException if there is no result to return * @throws jakarta.persistence.NoResultException if there is no result to return
*/ */
Object getSingleResult(); R getSingleResult();
/** /**
* Execute the query and return the single result of the query, * Execute the query and return the single result of the query,
@ -142,18 +142,18 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @throws NonUniqueResultException if there is more than one matching result * @throws NonUniqueResultException if there is more than one matching result
*/ */
Optional<?> uniqueResultOptional(); Optional<R> uniqueResultOptional();
SelectionQuery setHint(String hintName, Object value); SelectionQuery<R> setHint(String hintName, Object value);
@Override @Override
SelectionQuery setFlushMode(FlushModeType flushMode); SelectionQuery<R> setFlushMode(FlushModeType flushMode);
@Override @Override
SelectionQuery setHibernateFlushMode(FlushMode flushMode); SelectionQuery<R> setHibernateFlushMode(FlushMode flushMode);
@Override @Override
SelectionQuery setTimeout(int timeout); SelectionQuery<R> setTimeout(int timeout);
/** /**
* Obtain the JDBC fetch size hint in effect for this query. This value is eventually passed along to the JDBC * Obtain the JDBC fetch size hint in effect for this query. This value is eventually passed along to the JDBC
@ -179,7 +179,7 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @see #getFetchSize() * @see #getFetchSize()
*/ */
SelectionQuery setFetchSize(int fetchSize); SelectionQuery<R> setFetchSize(int fetchSize);
/** /**
* Should entities and proxies loaded by this Query be put in read-only mode? If the * Should entities and proxies loaded by this Query be put in read-only mode? If the
@ -225,7 +225,7 @@ public interface SelectionQuery extends CommonQueryContract {
* are to be put in read-only mode; {@code false} indicates that entities and proxies * are to be put in read-only mode; {@code false} indicates that entities and proxies
* loaded by the query will be put in modifiable mode * loaded by the query will be put in modifiable mode
*/ */
SelectionQuery setReadOnly(boolean readOnly); SelectionQuery<R> setReadOnly(boolean readOnly);
/** /**
* The max number of rows requested for the query results * The max number of rows requested for the query results
@ -236,7 +236,7 @@ public interface SelectionQuery extends CommonQueryContract {
* Set the max number of rows requested for the query results. Applied * Set the max number of rows requested for the query results. Applied
* to the SQL query * to the SQL query
*/ */
SelectionQuery setMaxResults(int maxResult); SelectionQuery<R> setMaxResults(int maxResult);
/** /**
* The first row position to return from the query results. Applied * The first row position to return from the query results. Applied
@ -248,7 +248,7 @@ public interface SelectionQuery extends CommonQueryContract {
* Set the first row position to return from the query results. Applied * Set the first row position to return from the query results. Applied
* to the SQL query * to the SQL query
*/ */
SelectionQuery setFirstResult(int startPosition); SelectionQuery<R> setFirstResult(int startPosition);
/** /**
* Obtain the CacheMode in effect for this query. By default, the query * Obtain the CacheMode in effect for this query. By default, the query
@ -275,7 +275,7 @@ public interface SelectionQuery extends CommonQueryContract {
* @see #getCacheMode() * @see #getCacheMode()
* @see Session#setCacheMode * @see Session#setCacheMode
*/ */
SelectionQuery setCacheMode(CacheMode cacheMode); SelectionQuery<R> setCacheMode(CacheMode cacheMode);
/** /**
* Should the results of the query be stored in the second level cache? * Should the results of the query be stored in the second level cache?
@ -296,7 +296,7 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @see #isCacheable * @see #isCacheable
*/ */
SelectionQuery setCacheable(boolean cacheable); SelectionQuery<R> setCacheable(boolean cacheable);
/** /**
* Obtain the name of the second level query cache region in which query results will be stored (if they are * Obtain the name of the second level query cache region in which query results will be stored (if they are
@ -311,7 +311,7 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @see #getCacheRegion() * @see #getCacheRegion()
*/ */
SelectionQuery setCacheRegion(String cacheRegion); SelectionQuery<R> setCacheRegion(String cacheRegion);
/** /**
* The LockOptions currently in effect for the query * The LockOptions currently in effect for the query
@ -323,134 +323,134 @@ public interface SelectionQuery extends CommonQueryContract {
* *
* @see #setHibernateLockMode * @see #setHibernateLockMode
*/ */
SelectionQuery setLockMode(LockModeType lockMode); SelectionQuery<R> setLockMode(LockModeType lockMode);
/** /**
* Specify the root LockMode for the query * Specify the root LockMode for the query
*/ */
SelectionQuery setHibernateLockMode(LockMode lockMode); SelectionQuery<R> setHibernateLockMode(LockMode lockMode);
/** /**
* Specify a LockMode to apply to a specific alias defined in the query * Specify a LockMode to apply to a specific alias defined in the query
*/ */
SelectionQuery setAliasSpecificLockMode(String alias, LockMode lockMode); SelectionQuery<R> setAliasSpecificLockMode(String alias, LockMode lockMode);
/** /**
* Specifies whether follow-on locking should be applied? * Specifies whether follow-on locking should be applied?
*/ */
SelectionQuery setFollowOnLocking(boolean enable); SelectionQuery<R> setFollowOnLocking(boolean enable);
@Override @Override
SelectionQuery setParameter(String name, Object value); SelectionQuery<R> setParameter(String name, Object value);
@Override @Override
<P> SelectionQuery setParameter(String name, P value, Class<P> type); <P> SelectionQuery<R> setParameter(String name, P value, Class<P> type);
@Override @Override
<P> SelectionQuery setParameter(String name, P value, BindableType<P> type); <P> SelectionQuery<R> setParameter(String name, P value, BindableType<P> type);
@Override @Override
SelectionQuery setParameter(String name, Instant value, TemporalType temporalType); SelectionQuery<R> setParameter(String name, Instant value, TemporalType temporalType);
@Override @Override
SelectionQuery setParameter(String name, Calendar value, TemporalType temporalType); SelectionQuery<R> setParameter(String name, Calendar value, TemporalType temporalType);
@Override @Override
SelectionQuery setParameter(String name, Date value, TemporalType temporalType); SelectionQuery<R> setParameter(String name, Date value, TemporalType temporalType);
@Override @Override
SelectionQuery setParameter(int position, Object value); SelectionQuery<R> setParameter(int position, Object value);
@Override @Override
<P> SelectionQuery setParameter(int position, P value, Class<P> type); <P> SelectionQuery<R> setParameter(int position, P value, Class<P> type);
@Override @Override
<P> SelectionQuery setParameter(int position, P value, BindableType<P> type); <P> SelectionQuery<R> setParameter(int position, P value, BindableType<P> type);
@Override @Override
SelectionQuery setParameter(int position, Instant value, TemporalType temporalType); SelectionQuery<R> setParameter(int position, Instant value, TemporalType temporalType);
@Override @Override
SelectionQuery setParameter(int position, Date value, TemporalType temporalType); SelectionQuery<R> setParameter(int position, Date value, TemporalType temporalType);
@Override @Override
SelectionQuery setParameter(int position, Calendar value, TemporalType temporalType); SelectionQuery<R> setParameter(int position, Calendar value, TemporalType temporalType);
@Override @Override
<T> SelectionQuery setParameter(QueryParameter<T> parameter, T value); <T> SelectionQuery<R> setParameter(QueryParameter<T> parameter, T value);
@Override @Override
<P> SelectionQuery setParameter(QueryParameter<P> parameter, P value, Class<P> type); <P> SelectionQuery<R> setParameter(QueryParameter<P> parameter, P value, Class<P> type);
@Override @Override
<P> SelectionQuery setParameter(QueryParameter<P> parameter, P val, BindableType<P> type); <P> SelectionQuery<R> setParameter(QueryParameter<P> parameter, P val, BindableType<P> type);
@Override @Override
<T> SelectionQuery setParameter(Parameter<T> param, T value); <T> SelectionQuery<R> setParameter(Parameter<T> param, T value);
@Override @Override
SelectionQuery setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType); SelectionQuery<R> setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType);
@Override @Override
SelectionQuery setParameter(Parameter<Date> param, Date value, TemporalType temporalType); SelectionQuery<R> setParameter(Parameter<Date> param, Date value, TemporalType temporalType);
@Override @Override
SelectionQuery setParameterList(String name, Collection values); SelectionQuery<R> setParameterList(String name, Collection values);
@Override @Override
<P> SelectionQuery setParameterList(String name, Collection<? extends P> values, Class<P> javaType); <P> SelectionQuery<R> setParameterList(String name, Collection<? extends P> values, Class<P> javaType);
@Override @Override
<P> SelectionQuery setParameterList(String name, Collection<? extends P> values, BindableType<P> type); <P> SelectionQuery<R> setParameterList(String name, Collection<? extends P> values, BindableType<P> type);
@Override @Override
SelectionQuery setParameterList(String name, Object[] values); SelectionQuery<R> setParameterList(String name, Object[] values);
@Override @Override
<P> SelectionQuery setParameterList(String name, P[] values, Class<P> javaType); <P> SelectionQuery<R> setParameterList(String name, P[] values, Class<P> javaType);
@Override @Override
<P> SelectionQuery setParameterList(String name, P[] values, BindableType<P> type); <P> SelectionQuery<R> setParameterList(String name, P[] values, BindableType<P> type);
@Override @Override
SelectionQuery setParameterList(int position, Collection values); SelectionQuery<R> setParameterList(int position, Collection values);
@Override @Override
<P> SelectionQuery setParameterList(int position, Collection<? extends P> values, Class<P> javaType); <P> SelectionQuery<R> setParameterList(int position, Collection<? extends P> values, Class<P> javaType);
@Override @Override
<P> SelectionQuery setParameterList(int position, Collection<? extends P> values, BindableType<P> type); <P> SelectionQuery<R> setParameterList(int position, Collection<? extends P> values, BindableType<P> type);
@Override @Override
SelectionQuery setParameterList(int position, Object[] values); SelectionQuery<R> setParameterList(int position, Object[] values);
@Override @Override
<P> SelectionQuery setParameterList(int position, P[] values, Class<P> javaType); <P> SelectionQuery<R> setParameterList(int position, P[] values, Class<P> javaType);
@Override @Override
<P> SelectionQuery setParameterList(int position, P[] values, BindableType<P> type); <P> SelectionQuery<R> setParameterList(int position, P[] values, BindableType<P> type);
@Override @Override
<P> SelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values); <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values);
@Override @Override
<P> SelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType); <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType);
@Override @Override
<P> SelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, BindableType<P> type); <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, BindableType<P> type);
@Override @Override
<P> SelectionQuery setParameterList(QueryParameter<P> parameter, P[] values); <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values);
@Override @Override
<P> SelectionQuery setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType); <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType);
@Override @Override
<P> SelectionQuery setParameterList(QueryParameter<P> parameter, P[] values, BindableType<P> type); <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values, BindableType<P> type);
@Override @Override
SelectionQuery setProperties(Object bean); SelectionQuery<R> setProperties(Object bean);
@Override @Override
SelectionQuery setProperties(@SuppressWarnings("rawtypes") Map bean); SelectionQuery<R> setProperties(@SuppressWarnings("rawtypes") Map bean);
} }

View File

@ -34,7 +34,6 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.EntityManagerMessageLogger; import org.hibernate.internal.EntityManagerMessageLogger;
import org.hibernate.internal.HEMLogging; import org.hibernate.internal.HEMLogging;
import org.hibernate.jpa.AvailableHints; import org.hibernate.jpa.AvailableHints;
import org.hibernate.jpa.internal.util.ConfigurationHelper;
import org.hibernate.jpa.internal.util.FlushModeTypeHelper; import org.hibernate.jpa.internal.util.FlushModeTypeHelper;
import org.hibernate.jpa.internal.util.LockModeTypeHelper; import org.hibernate.jpa.internal.util.LockModeTypeHelper;
import org.hibernate.metamodel.model.domain.ManagedDomainType; import org.hibernate.metamodel.model.domain.ManagedDomainType;
@ -77,8 +76,6 @@ import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_LOCK_TIMEOUT;
import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_QUERY_TIMEOUT; import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_QUERY_TIMEOUT;
import static org.hibernate.jpa.SpecHints.HINT_SPEC_CACHE_RETRIEVE_MODE; import static org.hibernate.jpa.SpecHints.HINT_SPEC_CACHE_RETRIEVE_MODE;
import static org.hibernate.jpa.SpecHints.HINT_SPEC_CACHE_STORE_MODE; import static org.hibernate.jpa.SpecHints.HINT_SPEC_CACHE_STORE_MODE;
import static org.hibernate.jpa.SpecHints.HINT_SPEC_FETCH_GRAPH;
import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOAD_GRAPH;
import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOCK_SCOPE; import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOCK_SCOPE;
import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOCK_TIMEOUT; import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOCK_TIMEOUT;
import static org.hibernate.jpa.SpecHints.HINT_SPEC_QUERY_TIMEOUT; import static org.hibernate.jpa.SpecHints.HINT_SPEC_QUERY_TIMEOUT;
@ -87,7 +84,7 @@ import static org.hibernate.jpa.SpecHints.HINT_SPEC_QUERY_TIMEOUT;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public abstract class AbstractQuery<R> public abstract class AbstractQuery<R>
extends AbstractSelectionQuery extends AbstractSelectionQuery<R>
implements QueryImplementor<R> { implements QueryImplementor<R> {
protected static final EntityManagerMessageLogger log = HEMLogging.messageLogger( AbstractQuery.class ); protected static final EntityManagerMessageLogger log = HEMLogging.messageLogger( AbstractQuery.class );

View File

@ -59,9 +59,9 @@ import static org.hibernate.jpa.QueryHints.HINT_READONLY;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public abstract class AbstractSelectionQuery public abstract class AbstractSelectionQuery<R>
extends AbstractCommonQueryContract extends AbstractCommonQueryContract
implements SelectionQuery, DomainQueryExecutionContext { implements SelectionQuery<R>, DomainQueryExecutionContext {
private Callback callback; private Callback callback;
@ -115,11 +115,11 @@ public abstract class AbstractSelectionQuery
private CacheMode sessionCacheMode; private CacheMode sessionCacheMode;
@Override @Override
public List list() { public List<R> list() {
beforeQuery(); beforeQuery();
boolean success = false; boolean success = false;
try { try {
final List result = doList(); final List<R> result = doList();
success = true; success = true;
return result; return result;
} }
@ -180,19 +180,19 @@ public abstract class AbstractSelectionQuery
return lockMode != null && lockMode.greaterThan( LockMode.READ ); return lockMode != null && lockMode.greaterThan( LockMode.READ );
} }
protected abstract List doList(); protected abstract List<R> doList();
@Override @Override
public ScrollableResultsImplementor scroll() { public ScrollableResultsImplementor<R> scroll() {
return scroll( getSession().getFactory().getJdbcServices().getJdbcEnvironment().getDialect().defaultScrollMode() ); return scroll( getSession().getFactory().getJdbcServices().getJdbcEnvironment().getDialect().defaultScrollMode() );
} }
@Override @Override
public ScrollableResultsImplementor scroll(ScrollMode scrollMode) { public ScrollableResultsImplementor<R> scroll(ScrollMode scrollMode) {
return doScroll( scrollMode ); return doScroll( scrollMode );
} }
protected abstract ScrollableResultsImplementor doScroll(ScrollMode scrollMode); protected abstract ScrollableResultsImplementor<R> doScroll(ScrollMode scrollMode);
@SuppressWarnings( {"unchecked", "rawtypes"} ) @SuppressWarnings( {"unchecked", "rawtypes"} )
@Override @Override
@ -206,14 +206,14 @@ public abstract class AbstractSelectionQuery
} }
@Override @Override
public Object uniqueResult() { public R uniqueResult() {
return uniqueElement( list() ); return uniqueElement( list() );
} }
@Override @Override
public Object getSingleResult() { public R getSingleResult() {
try { try {
final List<?> list = list(); final List<R> list = list();
if ( list.isEmpty() ) { if ( list.isEmpty() ) {
throw new NoResultException( "No result found for query" ); throw new NoResultException( "No result found for query" );
} }
@ -225,12 +225,12 @@ public abstract class AbstractSelectionQuery
} }
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
protected static Object uniqueElement(List<?> list) throws NonUniqueResultException { protected static <T> T uniqueElement(List<T> list) throws NonUniqueResultException {
int size = list.size(); int size = list.size();
if ( size == 0 ) { if ( size == 0 ) {
return null; return null;
} }
final Object first = list.get( 0 ); final T first = list.get( 0 );
// todo (6.0) : add a setting here to control whether to perform this validation or not // todo (6.0) : add a setting here to control whether to perform this validation or not
for ( int i = 1; i < size; i++ ) { for ( int i = 1; i < size; i++ ) {
if ( list.get( i ) != first ) { if ( list.get( i ) != first ) {
@ -240,9 +240,8 @@ public abstract class AbstractSelectionQuery
return first; return first;
} }
@SuppressWarnings("rawtypes")
@Override @Override
public Optional uniqueResultOptional() { public Optional<R> uniqueResultOptional() {
return Optional.ofNullable( uniqueResult() ); return Optional.ofNullable( uniqueResult() );
} }
@ -279,13 +278,13 @@ public abstract class AbstractSelectionQuery
} }
@Override @Override
public SelectionQuery setFlushMode(FlushModeType flushMode) { public SelectionQuery<R> setFlushMode(FlushModeType flushMode) {
getQueryOptions().setFlushMode( FlushMode.fromJpaFlushMode( flushMode ) ); getQueryOptions().setFlushMode( FlushMode.fromJpaFlushMode( flushMode ) );
return this; return this;
} }
@Override @Override
public SelectionQuery setMaxResults(int maxResult) { public SelectionQuery<R> setMaxResults(int maxResult) {
if ( maxResult < 0 ) { if ( maxResult < 0 ) {
throw new IllegalArgumentException( "max-results cannot be negative" ); throw new IllegalArgumentException( "max-results cannot be negative" );
} }
@ -298,12 +297,12 @@ public abstract class AbstractSelectionQuery
} }
@Override @Override
public SelectionQuery setFirstResult(int startPosition) { public SelectionQuery<R> setFirstResult(int startPosition) {
return null; return null;
} }
@Override @Override
public SelectionQuery setHint(String hintName, Object value) { public SelectionQuery<R> setHint(String hintName, Object value) {
super.setHint( hintName, value ); super.setHint( hintName, value );
return this; return this;
} }
@ -319,7 +318,7 @@ public abstract class AbstractSelectionQuery
* *
* @see #setHibernateLockMode * @see #setHibernateLockMode
*/ */
public SelectionQuery setLockMode(LockModeType lockMode) { public SelectionQuery<R> setLockMode(LockModeType lockMode) {
setHibernateLockMode( LockModeTypeHelper.getLockMode( lockMode ) ); setHibernateLockMode( LockModeTypeHelper.getLockMode( lockMode ) );
return this; return this;
} }
@ -327,7 +326,7 @@ public abstract class AbstractSelectionQuery
/** /**
* Specify the root LockMode for the query * Specify the root LockMode for the query
*/ */
public SelectionQuery setHibernateLockMode(LockMode lockMode) { public SelectionQuery<R> setHibernateLockMode(LockMode lockMode) {
getLockOptions().setLockMode( lockMode ); getLockOptions().setLockMode( lockMode );
return this; return this;
} }
@ -335,7 +334,7 @@ public abstract class AbstractSelectionQuery
/** /**
* Specify a LockMode to apply to a specific alias defined in the query * Specify a LockMode to apply to a specific alias defined in the query
*/ */
public SelectionQuery setAliasSpecificLockMode(String alias, LockMode lockMode) { public SelectionQuery<R> setAliasSpecificLockMode(String alias, LockMode lockMode) {
getLockOptions().setAliasSpecificLockMode( alias, lockMode ); getLockOptions().setAliasSpecificLockMode( alias, lockMode );
return this; return this;
} }
@ -343,7 +342,7 @@ public abstract class AbstractSelectionQuery
/** /**
* Specifies whether follow-on locking should be applied? * Specifies whether follow-on locking should be applied?
*/ */
public SelectionQuery setFollowOnLocking(boolean enable) { public SelectionQuery<R> setFollowOnLocking(boolean enable) {
getLockOptions().setFollowOnLocking( enable ); getLockOptions().setFollowOnLocking( enable );
return this; return this;
} }
@ -385,7 +384,7 @@ public abstract class AbstractSelectionQuery
} }
@Override @Override
public SelectionQuery setFetchSize(int fetchSize) { public SelectionQuery<R> setFetchSize(int fetchSize) {
getQueryOptions().setFetchSize( fetchSize ); getQueryOptions().setFetchSize( fetchSize );
return this; return this;
} }
@ -398,7 +397,7 @@ public abstract class AbstractSelectionQuery
} }
@Override @Override
public SelectionQuery setReadOnly(boolean readOnly) { public SelectionQuery<R> setReadOnly(boolean readOnly) {
getQueryOptions().setReadOnly( readOnly ); getQueryOptions().setReadOnly( readOnly );
return this; return this;
} }
@ -408,7 +407,7 @@ public abstract class AbstractSelectionQuery
} }
@Override @Override
public SelectionQuery setCacheMode(CacheMode cacheMode) { public SelectionQuery<R> setCacheMode(CacheMode cacheMode) {
getQueryOptions().setCacheMode( cacheMode ); getQueryOptions().setCacheMode( cacheMode );
return this; return this;
} }
@ -419,7 +418,7 @@ public abstract class AbstractSelectionQuery
} }
@Override @Override
public SelectionQuery setCacheable(boolean cacheable) { public SelectionQuery<R> setCacheable(boolean cacheable) {
getQueryOptions().setResultCachingEnabled( cacheable ); getQueryOptions().setResultCachingEnabled( cacheable );
return this; return this;
} }
@ -430,7 +429,7 @@ public abstract class AbstractSelectionQuery
} }
@Override @Override
public SelectionQuery setCacheRegion(String regionName) { public SelectionQuery<R> setCacheRegion(String regionName) {
getQueryOptions().setResultCacheRegionName( regionName ); getQueryOptions().setResultCacheRegionName( regionName );
return this; return this;
} }
@ -440,242 +439,242 @@ public abstract class AbstractSelectionQuery
// covariance // covariance
@Override @Override
public SelectionQuery setHibernateFlushMode(FlushMode flushMode) { public SelectionQuery<R> setHibernateFlushMode(FlushMode flushMode) {
super.setHibernateFlushMode( flushMode ); super.setHibernateFlushMode( flushMode );
return this; return this;
} }
@Override @Override
public SelectionQuery setTimeout(int timeout) { public SelectionQuery<R> setTimeout(int timeout) {
super.setTimeout( timeout ); super.setTimeout( timeout );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(String name, Object value) { public SelectionQuery<R> setParameter(String name, Object value) {
super.setParameter( name, value ); super.setParameter( name, value );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameter(String name, P value, Class<P> javaType) { public <P> SelectionQuery<R> setParameter(String name, P value, Class<P> javaType) {
super.setParameter( name, value, javaType ); super.setParameter( name, value, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameter(String name, P value, BindableType<P> type) { public <P> SelectionQuery<R> setParameter(String name, P value, BindableType<P> type) {
super.setParameter( name, value, type ); super.setParameter( name, value, type );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(String name, Instant value, TemporalType temporalType) { public SelectionQuery<R> setParameter(String name, Instant value, TemporalType temporalType) {
super.setParameter( name, value, temporalType ); super.setParameter( name, value, temporalType );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(int position, Object value) { public SelectionQuery<R> setParameter(int position, Object value) {
super.setParameter( position, value ); super.setParameter( position, value );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameter(int position, P value, Class<P> javaType) { public <P> SelectionQuery<R> setParameter(int position, P value, Class<P> javaType) {
super.setParameter( position, value, javaType ); super.setParameter( position, value, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameter(int position, P value, BindableType<P> type) { public <P> SelectionQuery<R> setParameter(int position, P value, BindableType<P> type) {
super.setParameter( position, value, type ); super.setParameter( position, value, type );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(int position, Instant value, TemporalType temporalType) { public SelectionQuery<R> setParameter(int position, Instant value, TemporalType temporalType) {
super.setParameter( position, value, temporalType ); super.setParameter( position, value, temporalType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameter(QueryParameter<P> parameter, P value) { public <P> SelectionQuery<R> setParameter(QueryParameter<P> parameter, P value) {
super.setParameter( parameter, value ); super.setParameter( parameter, value );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameter(QueryParameter<P> parameter, P value, Class<P> javaType) { public <P> SelectionQuery<R> setParameter(QueryParameter<P> parameter, P value, Class<P> javaType) {
super.setParameter( parameter, value, javaType ); super.setParameter( parameter, value, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameter(QueryParameter<P> parameter, P value, BindableType<P> type) { public <P> SelectionQuery<R> setParameter(QueryParameter<P> parameter, P value, BindableType<P> type) {
super.setParameter( parameter, value, type ); super.setParameter( parameter, value, type );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameter(Parameter<P> parameter, P value) { public <P> SelectionQuery<R> setParameter(Parameter<P> parameter, P value) {
super.setParameter( parameter, value ); super.setParameter( parameter, value );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) { public SelectionQuery<R> setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) {
super.setParameter( param, value, temporalType ); super.setParameter( param, value, temporalType );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(Parameter<Date> param, Date value, TemporalType temporalType) { public SelectionQuery<R> setParameter(Parameter<Date> param, Date value, TemporalType temporalType) {
super.setParameter( param, value, temporalType ); super.setParameter( param, value, temporalType );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(String name, Calendar value, TemporalType temporalType) { public SelectionQuery<R> setParameter(String name, Calendar value, TemporalType temporalType) {
super.setParameter( name, value, temporalType ); super.setParameter( name, value, temporalType );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(String name, Date value, TemporalType temporalType) { public SelectionQuery<R> setParameter(String name, Date value, TemporalType temporalType) {
super.setParameter( name, value, temporalType ); super.setParameter( name, value, temporalType );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(int position, Calendar value, TemporalType temporalType) { public SelectionQuery<R> setParameter(int position, Calendar value, TemporalType temporalType) {
super.setParameter( position, value, temporalType ); super.setParameter( position, value, temporalType );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameter(int position, Date value, TemporalType temporalType) { public SelectionQuery<R> setParameter(int position, Date value, TemporalType temporalType) {
super.setParameter( position, value, temporalType ); super.setParameter( position, value, temporalType );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameterList(String name, Collection values) { public SelectionQuery<R> setParameterList(String name, Collection values) {
super.setParameterList( name, values ); super.setParameterList( name, values );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(String name, Collection<? extends P> values, Class<P> javaType) { public <P> SelectionQuery<R> setParameterList(String name, Collection<? extends P> values, Class<P> javaType) {
super.setParameterList( name, values, javaType ); super.setParameterList( name, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(String name, Collection<? extends P> values, BindableType<P> type) { public <P> SelectionQuery<R> setParameterList(String name, Collection<? extends P> values, BindableType<P> type) {
super.setParameterList( name, values, type ); super.setParameterList( name, values, type );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameterList(String name, Object[] values) { public SelectionQuery<R> setParameterList(String name, Object[] values) {
super.setParameterList( name, values ); super.setParameterList( name, values );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(String name, P[] values, Class<P> javaType) { public <P> SelectionQuery<R> setParameterList(String name, P[] values, Class<P> javaType) {
super.setParameterList( name, values, javaType ); super.setParameterList( name, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(String name, P[] values, BindableType<P> type) { public <P> SelectionQuery<R> setParameterList(String name, P[] values, BindableType<P> type) {
super.setParameterList( name, values, type ); super.setParameterList( name, values, type );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameterList(int position, Collection values) { public SelectionQuery<R> setParameterList(int position, Collection values) {
super.setParameterList( position, values ); super.setParameterList( position, values );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(int position, Collection<? extends P> values, Class<P> javaType) { public <P> SelectionQuery<R> setParameterList(int position, Collection<? extends P> values, Class<P> javaType) {
super.setParameterList( position, values, javaType ); super.setParameterList( position, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(int position, Collection<? extends P> values, BindableType<P> type) { public <P> SelectionQuery<R> setParameterList(int position, Collection<? extends P> values, BindableType<P> type) {
super.setParameterList( position, values, type ); super.setParameterList( position, values, type );
return this; return this;
} }
@Override @Override
public SelectionQuery setParameterList(int position, Object[] values) { public SelectionQuery<R> setParameterList(int position, Object[] values) {
super.setParameterList( position, values ); super.setParameterList( position, values );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(int position, P[] values, Class<P> javaType) { public <P> SelectionQuery<R> setParameterList(int position, P[] values, Class<P> javaType) {
super.setParameterList( position, values, javaType ); super.setParameterList( position, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(int position, P[] values, BindableType<P> type) { public <P> SelectionQuery<R> setParameterList(int position, P[] values, BindableType<P> type) {
super.setParameterList( position, values, type ); super.setParameterList( position, values, type );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values) { public <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values) {
super.setParameterList( parameter, values ); super.setParameterList( parameter, values );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType) { public <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType) {
super.setParameterList( parameter, values, javaType ); super.setParameterList( parameter, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, BindableType<P> type) { public <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, BindableType<P> type) {
super.setParameterList( parameter, values, type ); super.setParameterList( parameter, values, type );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(QueryParameter<P> parameter, P[] values) { public <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values) {
super.setParameterList( parameter, values ); super.setParameterList( parameter, values );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType) { public <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType) {
super.setParameterList( parameter, values, javaType ); super.setParameterList( parameter, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SelectionQuery setParameterList(QueryParameter<P> parameter, P[] values, BindableType<P> type) { public <P> SelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values, BindableType<P> type) {
super.setParameterList( parameter, values, type ); super.setParameterList( parameter, values, type );
return this; return this;
} }
@Override @Override
public SelectionQuery setProperties(Map map) { public SelectionQuery<R> setProperties(Map map) {
super.setProperties( map ); super.setProperties( map );
return this; return this;
} }
@Override @Override
public SelectionQuery setProperties(Object bean) { public SelectionQuery<R> setProperties(Object bean) {
super.setProperties( bean ); super.setProperties( bean );
return this; return this;
} }

View File

@ -40,7 +40,6 @@ import org.hibernate.internal.AbstractSharedSessionContract;
import org.hibernate.internal.util.MathHelper; import org.hibernate.internal.util.MathHelper;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.jpa.internal.util.LockModeTypeHelper;
import org.hibernate.jpa.spi.NativeQueryTupleTransformer; import org.hibernate.jpa.spi.NativeQueryTupleTransformer;
import org.hibernate.metamodel.model.domain.BasicDomainType; import org.hibernate.metamodel.model.domain.BasicDomainType;
import org.hibernate.query.BindableType; import org.hibernate.query.BindableType;
@ -101,7 +100,6 @@ import jakarta.persistence.Tuple;
import jakarta.persistence.metamodel.SingularAttribute; import jakarta.persistence.metamodel.SingularAttribute;
import static org.hibernate.jpa.HibernateHints.HINT_NATIVE_LOCK_MODE; import static org.hibernate.jpa.HibernateHints.HINT_NATIVE_LOCK_MODE;
import static org.hibernate.jpa.QueryHints.HINT_NATIVE_SPACES;
/** /**
* @author Steve Ebersole * @author Steve Ebersole

View File

@ -25,140 +25,140 @@ import jakarta.persistence.TemporalType;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public interface SqmSelectionQuery extends SqmQuery, SelectionQuery { public interface SqmSelectionQuery<R> extends SqmQuery, SelectionQuery<R> {
@Override @Override
SqmSelectionQuery setParameter(String name, Object value); SqmSelectionQuery<R> setParameter(String name, Object value);
@Override @Override
<P> SqmSelectionQuery setParameter(String name, P value, Class<P> type); <P> SqmSelectionQuery<R> setParameter(String name, P value, Class<P> type);
@Override @Override
<P> SqmSelectionQuery setParameter(String name, P value, BindableType<P> type); <P> SqmSelectionQuery<R> setParameter(String name, P value, BindableType<P> type);
@Override @Override
SqmSelectionQuery setParameter(String name, Instant value, TemporalType temporalType); SqmSelectionQuery<R> setParameter(String name, Instant value, TemporalType temporalType);
@Override @Override
SqmSelectionQuery setParameter(String name, Calendar value, TemporalType temporalType); SqmSelectionQuery<R> setParameter(String name, Calendar value, TemporalType temporalType);
@Override @Override
SqmSelectionQuery setParameter(String name, Date value, TemporalType temporalType); SqmSelectionQuery<R> setParameter(String name, Date value, TemporalType temporalType);
@Override @Override
SqmSelectionQuery setParameter(int position, Object value); SqmSelectionQuery<R> setParameter(int position, Object value);
@Override @Override
<P> SqmSelectionQuery setParameter(int position, P value, Class<P> type); <P> SqmSelectionQuery<R> setParameter(int position, P value, Class<P> type);
@Override @Override
<P> SqmSelectionQuery setParameter(int position, P value, BindableType<P> type); <P> SqmSelectionQuery<R> setParameter(int position, P value, BindableType<P> type);
@Override @Override
SqmSelectionQuery setParameter(int position, Instant value, TemporalType temporalType); SqmSelectionQuery<R> setParameter(int position, Instant value, TemporalType temporalType);
@Override @Override
SqmSelectionQuery setParameter(int position, Date value, TemporalType temporalType); SqmSelectionQuery<R> setParameter(int position, Date value, TemporalType temporalType);
@Override @Override
SqmSelectionQuery setParameter(int position, Calendar value, TemporalType temporalType); SqmSelectionQuery<R> setParameter(int position, Calendar value, TemporalType temporalType);
@Override @Override
<T> SqmSelectionQuery setParameter(QueryParameter<T> parameter, T value); <T> SqmSelectionQuery<R> setParameter(QueryParameter<T> parameter, T value);
@Override @Override
<P> SqmSelectionQuery setParameter(QueryParameter<P> parameter, P value, Class<P> type); <P> SqmSelectionQuery<R> setParameter(QueryParameter<P> parameter, P value, Class<P> type);
@Override @Override
<P> SqmSelectionQuery setParameter(QueryParameter<P> parameter, P val, BindableType<P> type); <P> SqmSelectionQuery<R> setParameter(QueryParameter<P> parameter, P val, BindableType<P> type);
@Override @Override
<T> SqmSelectionQuery setParameter(Parameter<T> param, T value); <T> SqmSelectionQuery<R> setParameter(Parameter<T> param, T value);
@Override @Override
SqmSelectionQuery setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType); SqmSelectionQuery<R> setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType);
@Override @Override
SqmSelectionQuery setParameter(Parameter<Date> param, Date value, TemporalType temporalType); SqmSelectionQuery<R> setParameter(Parameter<Date> param, Date value, TemporalType temporalType);
@Override @Override
SqmSelectionQuery setParameterList(String name, Collection values); SqmSelectionQuery<R> setParameterList(String name, Collection values);
@Override @Override
<P> SqmSelectionQuery setParameterList(String name, Collection<? extends P> values, Class<P> javaType); <P> SqmSelectionQuery<R> setParameterList(String name, Collection<? extends P> values, Class<P> javaType);
@Override @Override
<P> SqmSelectionQuery setParameterList(String name, Collection<? extends P> values, BindableType<P> type); <P> SqmSelectionQuery<R> setParameterList(String name, Collection<? extends P> values, BindableType<P> type);
@Override @Override
SqmSelectionQuery setParameterList(String name, Object[] values); SqmSelectionQuery<R> setParameterList(String name, Object[] values);
@Override @Override
<P> SqmSelectionQuery setParameterList(String name, P[] values, Class<P> javaType); <P> SqmSelectionQuery<R> setParameterList(String name, P[] values, Class<P> javaType);
@Override @Override
<P> SqmSelectionQuery setParameterList(String name, P[] values, BindableType<P> type); <P> SqmSelectionQuery<R> setParameterList(String name, P[] values, BindableType<P> type);
@Override @Override
SqmSelectionQuery setParameterList(int position, Collection values); SqmSelectionQuery<R> setParameterList(int position, Collection values);
@Override @Override
<P> SqmSelectionQuery setParameterList(int position, Collection<? extends P> values, Class<P> javaType); <P> SqmSelectionQuery<R> setParameterList(int position, Collection<? extends P> values, Class<P> javaType);
@Override @Override
<P> SqmSelectionQuery setParameterList(int position, Collection<? extends P> values, BindableType<P> type); <P> SqmSelectionQuery<R> setParameterList(int position, Collection<? extends P> values, BindableType<P> type);
@Override @Override
SqmSelectionQuery setParameterList(int position, Object[] values); SqmSelectionQuery<R> setParameterList(int position, Object[] values);
@Override @Override
<P> SqmSelectionQuery setParameterList(int position, P[] values, Class<P> javaType); <P> SqmSelectionQuery<R> setParameterList(int position, P[] values, Class<P> javaType);
@Override @Override
<P> SqmSelectionQuery setParameterList(int position, P[] values, BindableType<P> type); <P> SqmSelectionQuery<R> setParameterList(int position, P[] values, BindableType<P> type);
@Override @Override
<P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values); <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values);
@Override @Override
<P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType); <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType);
@Override @Override
<P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, BindableType<P> type); <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, BindableType<P> type);
@Override @Override
<P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, P[] values); <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values);
@Override @Override
<P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType); <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType);
@Override @Override
<P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, P[] values, BindableType<P> type); <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values, BindableType<P> type);
@Override @Override
SqmSelectionQuery setProperties(Object bean); SqmSelectionQuery<R> setProperties(Object bean);
@Override @Override
SqmSelectionQuery setProperties(Map bean); SqmSelectionQuery<R> setProperties(Map bean);
@Override @Override
SqmSelectionQuery setHibernateFlushMode(FlushMode flushMode); SqmSelectionQuery<R> setHibernateFlushMode(FlushMode flushMode);
@Override @Override
SqmSelectionQuery setCacheMode(CacheMode cacheMode); SqmSelectionQuery<R> setCacheMode(CacheMode cacheMode);
@Override @Override
SqmSelectionQuery setCacheable(boolean cacheable); SqmSelectionQuery<R> setCacheable(boolean cacheable);
@Override @Override
SqmSelectionQuery setCacheRegion(String cacheRegion); SqmSelectionQuery<R> setCacheRegion(String cacheRegion);
@Override @Override
SqmSelectionQuery setTimeout(int timeout); SqmSelectionQuery<R> setTimeout(int timeout);
@Override @Override
SqmSelectionQuery setFetchSize(int fetchSize); SqmSelectionQuery<R> setFetchSize(int fetchSize);
@Override @Override
SqmSelectionQuery setReadOnly(boolean readOnly); SqmSelectionQuery<R> setReadOnly(boolean readOnly);
} }

View File

@ -107,16 +107,16 @@ import jakarta.persistence.PersistenceException;
import jakarta.persistence.TemporalType; import jakarta.persistence.TemporalType;
import jakarta.persistence.Tuple; import jakarta.persistence.Tuple;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_SHARED_CACHE_RETRIEVE_MODE; import static org.hibernate.jpa.HibernateHints.HINT_CACHEABLE;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_SHARED_CACHE_STORE_MODE; import static org.hibernate.jpa.HibernateHints.HINT_CACHE_MODE;
import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_RETRIEVE_MODE; import static org.hibernate.jpa.HibernateHints.HINT_CACHE_REGION;
import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_STORE_MODE; import static org.hibernate.jpa.HibernateHints.HINT_FETCH_SIZE;
import static org.hibernate.jpa.QueryHints.HINT_CACHEABLE; import static org.hibernate.jpa.HibernateHints.HINT_FOLLOW_ON_LOCKING;
import static org.hibernate.jpa.QueryHints.HINT_CACHE_MODE; import static org.hibernate.jpa.HibernateHints.HINT_READ_ONLY;
import static org.hibernate.jpa.QueryHints.HINT_CACHE_REGION; import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_CACHE_RETRIEVE_MODE;
import static org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE; import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_CACHE_STORE_MODE;
import static org.hibernate.jpa.QueryHints.HINT_FOLLOW_ON_LOCKING; import static org.hibernate.jpa.SpecHints.HINT_SPEC_CACHE_RETRIEVE_MODE;
import static org.hibernate.jpa.QueryHints.HINT_READONLY; import static org.hibernate.jpa.SpecHints.HINT_SPEC_CACHE_STORE_MODE;
import static org.hibernate.query.spi.SqlOmittingQueryOptions.omitSqlQueryOptions; import static org.hibernate.query.spi.SqlOmittingQueryOptions.omitSqlQueryOptions;
import static org.hibernate.query.sqm.internal.SqmUtil.isSelect; import static org.hibernate.query.sqm.internal.SqmUtil.isSelect;
@ -126,7 +126,7 @@ import static org.hibernate.query.sqm.internal.SqmUtil.isSelect;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class QuerySqmImpl<R> public class QuerySqmImpl<R>
extends AbstractSelectionQuery extends AbstractSelectionQuery<R>
implements SqmQueryImplementor<R>, InterpretationsKeySource, DomainQueryExecutionContext { implements SqmQueryImplementor<R>, InterpretationsKeySource, DomainQueryExecutionContext {
/** /**
@ -603,7 +603,7 @@ public class QuerySqmImpl<R>
} }
} }
protected List doList() { protected List<R> doList() {
verifySelect(); verifySelect();
getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) ); getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) );
@ -645,7 +645,7 @@ public class QuerySqmImpl<R>
executionContextToUse = this; executionContextToUse = this;
} }
final List<?> list = resolveSelectQueryPlan().performList( executionContextToUse ); final List<R> list = resolveSelectQueryPlan().performList( executionContextToUse );
if ( needsDistinct ) { if ( needsDistinct ) {
int includedCount = -1; int includedCount = -1;
@ -656,9 +656,9 @@ public class QuerySqmImpl<R>
final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null
? getMaxRows( sqmStatement, list.size() ) ? getMaxRows( sqmStatement, list.size() )
: getQueryOptions().getLimit().getMaxRows(); : getQueryOptions().getLimit().getMaxRows();
final List<Object> tmp = new ArrayList<>( list.size() ); final List<R> tmp = new ArrayList<>( list.size() );
final IdentitySet<Object> distinction = new IdentitySet<>( list.size() ); final IdentitySet<Object> distinction = new IdentitySet<>( list.size() );
for ( final Object result : list ) { for ( final R result : list ) {
if ( !distinction.add( result ) ) { if ( !distinction.add( result ) ) {
continue; continue;
} }
@ -687,7 +687,7 @@ public class QuerySqmImpl<R>
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Select query plan // Select query plan
private SelectQueryPlan<?> resolveSelectQueryPlan() { private SelectQueryPlan<R> resolveSelectQueryPlan() {
final QueryInterpretationCache.Key cacheKey = SqmInterpretationsKey.createInterpretationsKey( this ); final QueryInterpretationCache.Key cacheKey = SqmInterpretationsKey.createInterpretationsKey( this );
if ( cacheKey != null ) { if ( cacheKey != null ) {
return getSession().getFactory().getQueryEngine().getInterpretationCache().resolveSelectQueryPlan( return getSession().getFactory().getQueryEngine().getInterpretationCache().resolveSelectQueryPlan(
@ -700,9 +700,9 @@ public class QuerySqmImpl<R>
} }
} }
private SelectQueryPlan<?> buildSelectQueryPlan() { private SelectQueryPlan<R> buildSelectQueryPlan() {
final SqmSelectStatement<?>[] concreteSqmStatements = QuerySplitter.split( final SqmSelectStatement<R>[] concreteSqmStatements = QuerySplitter.split(
(SqmSelectStatement<?>) getSqmStatement(), (SqmSelectStatement<R>) getSqmStatement(),
getSession().getFactory() getSession().getFactory()
); );
@ -714,8 +714,9 @@ public class QuerySqmImpl<R>
} }
} }
private SelectQueryPlan<?> buildAggregatedSelectQueryPlan(SqmSelectStatement<?>[] concreteSqmStatements) { private SelectQueryPlan<R> buildAggregatedSelectQueryPlan(SqmSelectStatement<?>[] concreteSqmStatements) {
final SelectQueryPlan<?>[] aggregatedQueryPlans = new SelectQueryPlan[ concreteSqmStatements.length ]; //noinspection unchecked
final SelectQueryPlan<R>[] aggregatedQueryPlans = new SelectQueryPlan[ concreteSqmStatements.length ];
// todo (6.0) : we want to make sure that certain thing (ResultListTransformer, etc) only get applied at the aggregator-level // todo (6.0) : we want to make sure that certain thing (ResultListTransformer, etc) only get applied at the aggregator-level
@ -727,12 +728,12 @@ public class QuerySqmImpl<R>
); );
} }
return new AggregatedSelectQueryPlanImpl( aggregatedQueryPlans ); return new AggregatedSelectQueryPlanImpl<>( aggregatedQueryPlans );
} }
private <R> SelectQueryPlan<R> buildConcreteSelectQueryPlan( private <T> SelectQueryPlan<T> buildConcreteSelectQueryPlan(
SqmSelectStatement<?> concreteSqmStatement, SqmSelectStatement<?> concreteSqmStatement,
Class<R> resultType, Class<T> resultType,
QueryOptions queryOptions) { QueryOptions queryOptions) {
return new ConcreteSqmSelectQueryPlan<>( return new ConcreteSqmSelectQueryPlan<>(
concreteSqmStatement, concreteSqmStatement,
@ -1000,7 +1001,7 @@ public class QuerySqmImpl<R>
super.collectHints( hints ); super.collectHints( hints );
if ( isReadOnly() ) { if ( isReadOnly() ) {
hints.put( HINT_READONLY, true ); hints.put( HINT_READ_ONLY, true );
} }
putIfNotNull( hints, HINT_FETCH_SIZE, getFetchSize() ); putIfNotNull( hints, HINT_FETCH_SIZE, getFetchSize() );
@ -1008,14 +1009,12 @@ public class QuerySqmImpl<R>
if ( isCacheable() ) { if ( isCacheable() ) {
hints.put( HINT_CACHEABLE, true ); hints.put( HINT_CACHEABLE, true );
putIfNotNull( hints, HINT_CACHE_REGION, getCacheRegion() ); putIfNotNull( hints, HINT_CACHE_REGION, getCacheRegion() );
putIfNotNull( hints, HINT_CACHE_MODE, getCacheMode() ); putIfNotNull( hints, HINT_CACHE_MODE, getCacheMode() );
putIfNotNull( hints, JAKARTA_SHARED_CACHE_RETRIEVE_MODE, getQueryOptions().getCacheRetrieveMode() );
putIfNotNull( hints, JAKARTA_SHARED_CACHE_STORE_MODE, getQueryOptions().getCacheStoreMode() ); putIfNotNull( hints, HINT_SPEC_CACHE_RETRIEVE_MODE, getQueryOptions().getCacheRetrieveMode() );
//noinspection deprecation putIfNotNull( hints, HINT_SPEC_CACHE_STORE_MODE, getQueryOptions().getCacheStoreMode() );
putIfNotNull( hints, JPA_SHARED_CACHE_RETRIEVE_MODE, getQueryOptions().getCacheRetrieveMode() ); putIfNotNull( hints, HINT_JAVAEE_CACHE_RETRIEVE_MODE, getQueryOptions().getCacheRetrieveMode() );
//noinspection deprecation putIfNotNull( hints, HINT_JAVAEE_CACHE_STORE_MODE, getQueryOptions().getCacheStoreMode() );
putIfNotNull( hints, JPA_SHARED_CACHE_STORE_MODE, getQueryOptions().getCacheStoreMode() );
} }
final AppliedGraph appliedGraph = getQueryOptions().getAppliedGraph(); final AppliedGraph appliedGraph = getQueryOptions().getAppliedGraph();

View File

@ -47,6 +47,7 @@ import org.hibernate.query.spi.SelectQueryPlan;
import org.hibernate.query.sqm.SqmSelectionQuery; import org.hibernate.query.sqm.SqmSelectionQuery;
import org.hibernate.query.sqm.internal.SqmInterpretationsKey.InterpretationsKeySource; import org.hibernate.query.sqm.internal.SqmInterpretationsKey.InterpretationsKeySource;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement; import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelection;
import jakarta.persistence.FlushModeType; import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType; import jakarta.persistence.LockModeType;
@ -68,7 +69,7 @@ import static org.hibernate.query.spi.SqlOmittingQueryOptions.omitSqlQueryOption
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSelectionQuery, InterpretationsKeySource { public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implements SqmSelectionQuery<R>, InterpretationsKeySource {
private final String hql; private final String hql;
private final SqmSelectStatement<?> sqm; private final SqmSelectStatement<?> sqm;
@ -76,9 +77,9 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
private final DomainParameterXref domainParameterXref; private final DomainParameterXref domainParameterXref;
private final QueryParameterBindingsImpl parameterBindings; private final QueryParameterBindingsImpl parameterBindings;
private final Class<?> resultType; private final Class<R> resultType;
public SqmSelectQueryImpl( public SqmSelectionQueryImpl(
String hql, String hql,
HqlInterpretation hqlInterpretation, HqlInterpretation hqlInterpretation,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
@ -91,13 +92,23 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
this.parameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, session.getFactory() ); this.parameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, session.getFactory() );
this.resultType = sqm.getQuerySpec().getSelectClause().getSelections().size() > 1 this.resultType = determineResultType( sqm );
? Object[].class
: Object.class;
setComment( hql ); setComment( hql );
} }
private static <T> Class<T> determineResultType(SqmSelectStatement<?> sqm) {
final List<SqmSelection<?>> selections = sqm.getQuerySpec().getSelectClause().getSelections();
if ( selections.size() == 1 ) {
final SqmSelection<?> sqmSelection = selections.get( 0 );
//noinspection unchecked
return (Class<T>) sqmSelection.getNodeJavaType().getJavaTypeClass();
}
//noinspection unchecked
return (Class<T>) Object[].class;
}
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public SqmSelectStatement getSqmStatement() { public SqmSelectStatement getSqmStatement() {
return sqm; return sqm;
@ -136,7 +147,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
resetCallback(); resetCallback();
} }
protected List doList() { protected List<R> doList() {
getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) ); getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) );
final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement(); final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement();
@ -177,7 +188,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
executionContextToUse = this; executionContextToUse = this;
} }
final List<?> list = resolveQueryPlan().performList( executionContextToUse ); final List<R> list = resolveQueryPlan().performList( executionContextToUse );
if ( needsDistinct ) { if ( needsDistinct ) {
int includedCount = -1; int includedCount = -1;
@ -188,9 +199,9 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null
? getMaxRows( sqmStatement, list.size() ) ? getMaxRows( sqmStatement, list.size() )
: getQueryOptions().getLimit().getMaxRows(); : getQueryOptions().getLimit().getMaxRows();
final List<Object> tmp = new ArrayList<>( list.size() ); final List<R> tmp = new ArrayList<>( list.size() );
final IdentitySet<Object> distinction = new IdentitySet<>( list.size() ); final IdentitySet<Object> distinction = new IdentitySet<>( list.size() );
for ( final Object result : list ) { for ( final R result : list ) {
if ( !distinction.add( result ) ) { if ( !distinction.add( result ) ) {
continue; continue;
} }
@ -210,7 +221,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
@Override @Override
protected ScrollableResultsImplementor doScroll(ScrollMode scrollMode) { protected ScrollableResultsImplementor<R> doScroll(ScrollMode scrollMode) {
getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) ); getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) );
return resolveQueryPlan().performScroll( scrollMode, this ); return resolveQueryPlan().performScroll( scrollMode, this );
@ -220,7 +231,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Query plan // Query plan
private SelectQueryPlan<?> resolveQueryPlan() { private SelectQueryPlan<R> resolveQueryPlan() {
final QueryInterpretationCache.Key cacheKey = SqmInterpretationsKey.createInterpretationsKey( this ); final QueryInterpretationCache.Key cacheKey = SqmInterpretationsKey.createInterpretationsKey( this );
if ( cacheKey != null ) { if ( cacheKey != null ) {
return getSession().getFactory().getQueryEngine().getInterpretationCache().resolveSelectQueryPlan( return getSession().getFactory().getQueryEngine().getInterpretationCache().resolveSelectQueryPlan(
@ -233,7 +244,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
} }
private SelectQueryPlan<?> buildQueryPlan() { private SelectQueryPlan<R> buildQueryPlan() {
final SqmSelectStatement<?>[] concreteSqmStatements = QuerySplitter.split( final SqmSelectStatement<?>[] concreteSqmStatements = QuerySplitter.split(
(SqmSelectStatement<?>) getSqmStatement(), (SqmSelectStatement<?>) getSqmStatement(),
getSession().getFactory() getSession().getFactory()
@ -247,8 +258,9 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
} }
private SelectQueryPlan<?> buildAggregatedQueryPlan(SqmSelectStatement<?>[] concreteSqmStatements) { private SelectQueryPlan<R> buildAggregatedQueryPlan(SqmSelectStatement<?>[] concreteSqmStatements) {
final SelectQueryPlan<?>[] aggregatedQueryPlans = new SelectQueryPlan[ concreteSqmStatements.length ]; //noinspection unchecked
final SelectQueryPlan<R>[] aggregatedQueryPlans = new SelectQueryPlan[ concreteSqmStatements.length ];
// todo (6.0) : we want to make sure that certain thing (ResultListTransformer, etc) only get applied at the aggregator-level // todo (6.0) : we want to make sure that certain thing (ResultListTransformer, etc) only get applied at the aggregator-level
@ -260,12 +272,12 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
); );
} }
return new AggregatedSelectQueryPlanImpl( aggregatedQueryPlans ); return new AggregatedSelectQueryPlanImpl<>( aggregatedQueryPlans );
} }
private <R> SelectQueryPlan<R> buildConcreteQueryPlan( private <T> SelectQueryPlan<T> buildConcreteQueryPlan(
SqmSelectStatement<?> concreteSqmStatement, SqmSelectStatement<?> concreteSqmStatement,
Class<R> resultType, Class<T> resultType,
QueryOptions queryOptions) { QueryOptions queryOptions) {
return new ConcreteSqmSelectQueryPlan<>( return new ConcreteSqmSelectQueryPlan<>(
concreteSqmStatement, concreteSqmStatement,
@ -282,7 +294,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
// InterpretationsKeySource // InterpretationsKeySource
@Override @Override
public Class<?> getResultType() { public Class<R> getResultType() {
return resultType; return resultType;
} }
@ -317,7 +329,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
@Override @Override
public SqmSelectionQuery setFlushMode(FlushModeType flushMode) { public SqmSelectionQuery<R> setFlushMode(FlushModeType flushMode) {
setHibernateFlushMode( FlushModeTypeHelper.getFlushMode( flushMode ) ); setHibernateFlushMode( FlushModeTypeHelper.getFlushMode( flushMode ) );
return this; return this;
} }
@ -333,7 +345,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
* @see #setHibernateLockMode * @see #setHibernateLockMode
*/ */
@Override @Override
public SqmSelectionQuery setLockMode(LockModeType lockMode) { public SqmSelectionQuery<R> setLockMode(LockModeType lockMode) {
setHibernateLockMode( LockModeTypeHelper.getLockMode( lockMode ) ); setHibernateLockMode( LockModeTypeHelper.getLockMode( lockMode ) );
return this; return this;
} }
@ -342,7 +354,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
* Specify the root LockMode for the query * Specify the root LockMode for the query
*/ */
@Override @Override
public SqmSelectionQuery setHibernateLockMode(LockMode lockMode) { public SqmSelectionQuery<R> setHibernateLockMode(LockMode lockMode) {
getLockOptions().setLockMode( lockMode ); getLockOptions().setLockMode( lockMode );
return this; return this;
} }
@ -351,7 +363,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
* Specify a LockMode to apply to a specific alias defined in the query * Specify a LockMode to apply to a specific alias defined in the query
*/ */
@Override @Override
public SqmSelectionQuery setAliasSpecificLockMode(String alias, LockMode lockMode) { public SqmSelectionQuery<R> setAliasSpecificLockMode(String alias, LockMode lockMode) {
getLockOptions().setAliasSpecificLockMode( alias, lockMode ); getLockOptions().setAliasSpecificLockMode( alias, lockMode );
return this; return this;
} }
@ -360,7 +372,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
* Specifies whether follow-on locking should be applied? * Specifies whether follow-on locking should be applied?
*/ */
@Override @Override
public SqmSelectionQuery setFollowOnLocking(boolean enable) { public SqmSelectionQuery<R> setFollowOnLocking(boolean enable) {
getLockOptions().setFollowOnLocking( enable ); getLockOptions().setFollowOnLocking( enable );
return this; return this;
} }
@ -371,7 +383,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
@Override @Override
public SqmSelectionQuery setFetchSize(int fetchSize) { public SqmSelectionQuery<R> setFetchSize(int fetchSize) {
getQueryOptions().setFetchSize( fetchSize ); getQueryOptions().setFetchSize( fetchSize );
return this; return this;
} }
@ -384,7 +396,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
@Override @Override
public SqmSelectionQuery setReadOnly(boolean readOnly) { public SqmSelectionQuery<R> setReadOnly(boolean readOnly) {
getQueryOptions().setReadOnly( readOnly ); getQueryOptions().setReadOnly( readOnly );
return this; return this;
} }
@ -394,7 +406,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
@Override @Override
public SqmSelectionQuery setCacheMode(CacheMode cacheMode) { public SqmSelectionQuery<R> setCacheMode(CacheMode cacheMode) {
getQueryOptions().setCacheMode( cacheMode ); getQueryOptions().setCacheMode( cacheMode );
return this; return this;
} }
@ -405,7 +417,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
@Override @Override
public SqmSelectionQuery setCacheable(boolean cacheable) { public SqmSelectionQuery<R> setCacheable(boolean cacheable) {
getQueryOptions().setResultCachingEnabled( cacheable ); getQueryOptions().setResultCachingEnabled( cacheable );
return this; return this;
} }
@ -416,7 +428,7 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
} }
@Override @Override
public SqmSelectionQuery setCacheRegion(String regionName) { public SqmSelectionQuery<R> setCacheRegion(String regionName) {
getQueryOptions().setResultCacheRegionName( regionName ); getQueryOptions().setResultCacheRegionName( regionName );
return this; return this;
} }
@ -461,242 +473,242 @@ public class SqmSelectQueryImpl extends AbstractSelectionQuery implements SqmSel
// covariance // covariance
@Override @Override
public SqmSelectionQuery setHibernateFlushMode(FlushMode flushMode) { public SqmSelectionQuery<R> setHibernateFlushMode(FlushMode flushMode) {
super.setHibernateFlushMode( flushMode ); super.setHibernateFlushMode( flushMode );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setTimeout(int timeout) { public SqmSelectionQuery<R> setTimeout(int timeout) {
super.setTimeout( timeout ); super.setTimeout( timeout );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(String name, Object value) { public SqmSelectionQuery<R> setParameter(String name, Object value) {
super.setParameter( name, value ); super.setParameter( name, value );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameter(String name, P value, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameter(String name, P value, Class<P> javaType) {
super.setParameter( name, value, javaType ); super.setParameter( name, value, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameter(String name, P value, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameter(String name, P value, BindableType<P> type) {
super.setParameter( name, value, type ); super.setParameter( name, value, type );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(String name, Instant value, TemporalType temporalType) { public SqmSelectionQuery<R> setParameter(String name, Instant value, TemporalType temporalType) {
super.setParameter( name, value, temporalType ); super.setParameter( name, value, temporalType );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(int position, Object value) { public SqmSelectionQuery<R> setParameter(int position, Object value) {
super.setParameter( position, value ); super.setParameter( position, value );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameter(int position, P value, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameter(int position, P value, Class<P> javaType) {
super.setParameter( position, value, javaType ); super.setParameter( position, value, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameter(int position, P value, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameter(int position, P value, BindableType<P> type) {
super.setParameter( position, value, type ); super.setParameter( position, value, type );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(int position, Instant value, TemporalType temporalType) { public SqmSelectionQuery<R> setParameter(int position, Instant value, TemporalType temporalType) {
super.setParameter( position, value, temporalType ); super.setParameter( position, value, temporalType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameter(QueryParameter<P> parameter, P value) { public <P> SqmSelectionQuery<R> setParameter(QueryParameter<P> parameter, P value) {
super.setParameter( parameter, value ); super.setParameter( parameter, value );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameter(QueryParameter<P> parameter, P value, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameter(QueryParameter<P> parameter, P value, Class<P> javaType) {
super.setParameter( parameter, value, javaType ); super.setParameter( parameter, value, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameter(QueryParameter<P> parameter, P value, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameter(QueryParameter<P> parameter, P value, BindableType<P> type) {
super.setParameter( parameter, value, type ); super.setParameter( parameter, value, type );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameter(Parameter<P> parameter, P value) { public <P> SqmSelectionQuery<R> setParameter(Parameter<P> parameter, P value) {
super.setParameter( parameter, value ); super.setParameter( parameter, value );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) { public SqmSelectionQuery<R> setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) {
super.setParameter( param, value, temporalType ); super.setParameter( param, value, temporalType );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(Parameter<Date> param, Date value, TemporalType temporalType) { public SqmSelectionQuery<R> setParameter(Parameter<Date> param, Date value, TemporalType temporalType) {
super.setParameter( param, value, temporalType ); super.setParameter( param, value, temporalType );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(String name, Calendar value, TemporalType temporalType) { public SqmSelectionQuery<R> setParameter(String name, Calendar value, TemporalType temporalType) {
super.setParameter( name, value, temporalType ); super.setParameter( name, value, temporalType );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(String name, Date value, TemporalType temporalType) { public SqmSelectionQuery<R> setParameter(String name, Date value, TemporalType temporalType) {
super.setParameter( name, value, temporalType ); super.setParameter( name, value, temporalType );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(int position, Calendar value, TemporalType temporalType) { public SqmSelectionQuery<R> setParameter(int position, Calendar value, TemporalType temporalType) {
super.setParameter( position, value, temporalType ); super.setParameter( position, value, temporalType );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameter(int position, Date value, TemporalType temporalType) { public SqmSelectionQuery<R> setParameter(int position, Date value, TemporalType temporalType) {
super.setParameter( position, value, temporalType ); super.setParameter( position, value, temporalType );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameterList(String name, Collection values) { public SqmSelectionQuery<R> setParameterList(String name, Collection values) {
super.setParameterList( name, values ); super.setParameterList( name, values );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(String name, Collection<? extends P> values, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameterList(String name, Collection<? extends P> values, Class<P> javaType) {
super.setParameterList( name, values, javaType ); super.setParameterList( name, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(String name, Collection<? extends P> values, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameterList(String name, Collection<? extends P> values, BindableType<P> type) {
super.setParameterList( name, values, type ); super.setParameterList( name, values, type );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameterList(String name, Object[] values) { public SqmSelectionQuery<R> setParameterList(String name, Object[] values) {
super.setParameterList( name, values ); super.setParameterList( name, values );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(String name, P[] values, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameterList(String name, P[] values, Class<P> javaType) {
super.setParameterList( name, values, javaType ); super.setParameterList( name, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(String name, P[] values, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameterList(String name, P[] values, BindableType<P> type) {
super.setParameterList( name, values, type ); super.setParameterList( name, values, type );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameterList(int position, Collection values) { public SqmSelectionQuery<R> setParameterList(int position, Collection values) {
super.setParameterList( position, values ); super.setParameterList( position, values );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(int position, Collection<? extends P> values, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameterList(int position, Collection<? extends P> values, Class<P> javaType) {
super.setParameterList( position, values, javaType ); super.setParameterList( position, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(int position, Collection<? extends P> values, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameterList(int position, Collection<? extends P> values, BindableType<P> type) {
super.setParameterList( position, values, type ); super.setParameterList( position, values, type );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setParameterList(int position, Object[] values) { public SqmSelectionQuery<R> setParameterList(int position, Object[] values) {
super.setParameterList( position, values ); super.setParameterList( position, values );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(int position, P[] values, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameterList(int position, P[] values, Class<P> javaType) {
super.setParameterList( position, values, javaType ); super.setParameterList( position, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(int position, P[] values, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameterList(int position, P[] values, BindableType<P> type) {
super.setParameterList( position, values, type ); super.setParameterList( position, values, type );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values) { public <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values) {
super.setParameterList( parameter, values ); super.setParameterList( parameter, values );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType) {
super.setParameterList( parameter, values, javaType ); super.setParameterList( parameter, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, BindableType<P> type) {
super.setParameterList( parameter, values, type ); super.setParameterList( parameter, values, type );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, P[] values) { public <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values) {
super.setParameterList( parameter, values ); super.setParameterList( parameter, values );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType) { public <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType) {
super.setParameterList( parameter, values, javaType ); super.setParameterList( parameter, values, javaType );
return this; return this;
} }
@Override @Override
public <P> SqmSelectionQuery setParameterList(QueryParameter<P> parameter, P[] values, BindableType<P> type) { public <P> SqmSelectionQuery<R> setParameterList(QueryParameter<P> parameter, P[] values, BindableType<P> type) {
super.setParameterList( parameter, values, type ); super.setParameterList( parameter, values, type );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setProperties(Map map) { public SqmSelectionQuery<R> setProperties(Map map) {
super.setProperties( map ); super.setProperties( map );
return this; return this;
} }
@Override @Override
public SqmSelectionQuery setProperties(Object bean) { public SqmSelectionQuery<R> setProperties(Object bean) {
super.setProperties( bean ); super.setProperties( bean );
return this; return this;
} }

View File

@ -11,6 +11,7 @@ import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.query.SelectionQuery; import org.hibernate.query.SelectionQuery;
import org.hibernate.testing.orm.domain.StandardDomainModel; import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.domain.contacts.Contact;
import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -23,7 +24,17 @@ import org.junit.jupiter.api.Test;
@SessionFactory @SessionFactory
public class BasicUntypedQueryTests { public class BasicUntypedQueryTests {
@Test @Test
public void untypedEntitySelectTest(SessionFactoryScope scope) { public void typedEntitySelectTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
checkResults(
session.createSelectQuery( "select c from Contact c", Contact.class ),
session
);
} );
}
@Test
public void rawEntitySelectTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> { scope.inTransaction( (session) -> {
checkResults( checkResults(
session.createSelectQuery( "select c from Contact c" ), session.createSelectQuery( "select c from Contact c" ),
@ -32,7 +43,31 @@ public class BasicUntypedQueryTests {
} ); } );
} }
private void checkResults(SelectionQuery query, SessionImplementor session) { @Test
public void rawScalarSelectTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
checkResults(
session.createSelectQuery( "select c.name from Contact c" ),
session
);
} );
}
@Test
public void typedScalarSelectTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
checkResults(
session.createSelectQuery( "select c.name from Contact c", Contact.Name.class ),
session
);
checkResults(
session.createSelectQuery( "select c.name.first from Contact c", String.class ),
session
);
} );
}
private void checkResults(SelectionQuery<?> query, SessionImplementor session) {
query.list(); query.list();
query.getResultList(); query.getResultList();
query.uniqueResult(); query.uniqueResult();
@ -41,14 +76,4 @@ public class BasicUntypedQueryTests {
query.scroll( ScrollMode.SCROLL_SENSITIVE ).close(); query.scroll( ScrollMode.SCROLL_SENSITIVE ).close();
query.stream().close(); query.stream().close();
} }
@Test
public void untypedScalarSelectTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
checkResults(
session.createSelectQuery( "select c.name from Contact c" ),
session
);
} );
}
} }