Skip to content

HHH-19364 begin migrating Processor to XxxxxSpecification #10067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,6 @@ public interface StatelessSession extends SharedSessionContract {
*
* @param entityGraph The {@link EntityGraph}, interpreted as a
* {@linkplain org.hibernate.graph.GraphSemantic#LOAD load graph}
* @param graphSemantic a {@link GraphSemantic} specifying
* how the graph should be interpreted
* @param ids The ids of the entities to retrieve
* @return an ordered list of detached entity instances, with
* null elements representing missing entities
Expand Down
4 changes: 0 additions & 4 deletions hibernate-core/src/main/java/org/hibernate/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.graph.GraphSemantic;
import org.hibernate.graph.RootGraph;
import org.hibernate.query.restriction.Restriction;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.transform.ResultTransformer;

Expand Down Expand Up @@ -935,9 +934,6 @@ default Query<R> setPage(Page page) {
@Override @Incubating
Query<R> setOrder(Order<? super R> order);

@Override @Incubating
Query<R> addRestriction(Restriction<? super R> restriction);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// deprecated methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import jakarta.persistence.TemporalType;
import org.hibernate.engine.profile.DefaultFetchProfile;
import org.hibernate.graph.GraphSemantic;
import org.hibernate.query.restriction.Restriction;

/**
* Within the context of an active {@linkplain org.hibernate.Session session},
Expand Down Expand Up @@ -623,19 +622,6 @@ default Stream<R> stream() {
@Incubating
SelectionQuery<R> setOrder(Order<? super R> order);

/**
* If the result type of this query is an entity class, add a
* {@linkplain Restriction rule} for restricting the query results.
*
* @param restriction an instance of {@link Restriction}
*
* @see Restriction
*
* @since 7.0
*/
@Incubating
SelectionQuery<R> addRestriction(Restriction<? super R> restriction);

/**
* Specifies whether follow-on locking should be applied
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.hibernate.Incubating;
import org.hibernate.Internal;
import org.hibernate.query.Order;
import org.hibernate.query.SelectionQuery;
import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.hibernate.query.range.Range;

Expand All @@ -21,7 +20,7 @@
/**
* A rule for restricting query results. This allows restrictions to be added to
* a {@link org.hibernate.query.programmatic.SelectionSpecification} by calling
* {@link SelectionQuery#addRestriction(Restriction)}.
* {@link org.hibernate.query.programmatic.SelectionSpecification#restrict(Restriction)}.
* <pre>
* SelectionSpecification.create(Book.class)
* .restrict(Restriction.like(Book_.title, "%Hibernate%", false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.hibernate.query.Order;
import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.restriction.Restriction;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.named.NamedQueryMemento;
Expand Down Expand Up @@ -301,11 +300,6 @@ public Query<R> setOrder(Order<? super R> order) {
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
}

@Override
public Query<R> addRestriction(Restriction<? super R> restriction) {
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
}

@Override
public String getComment() {
return super.getComment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import org.hibernate.query.PathException;
import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.restriction.Restriction;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.internal.DelegatingDomainQueryExecutionContext;
Expand Down Expand Up @@ -1662,11 +1661,6 @@ public Query<R> setOrder(Order<? super R> order) {
throw new UnsupportedOperationException("Ordering not currently supported for native queries");
}

@Override
public Query<R> addRestriction(Restriction<? super R> restriction) {
throw new UnsupportedOperationException("Restrictions not currently supported for native queries");
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Hints

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.hibernate.query.Order;
import org.hibernate.query.Page;
import org.hibernate.query.QueryLogging;
import org.hibernate.query.restriction.Restriction;
import org.hibernate.query.SelectionQuery;
import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.hql.internal.QuerySplitter;
Expand Down Expand Up @@ -205,16 +204,16 @@ public SelectionQuery<R> setOrder(Order<? super R> order) {
return this;
}

@Override
public SelectionQuery<R> addRestriction(Restriction<? super R> restriction) {
final SqmSelectStatement<R> selectStatement = getSqmSelectStatement().copy( noParamCopyContext( SqmQuerySource.CRITERIA ) );
restriction.apply( selectStatement, selectStatement.<R>getRoot( 0, getExpectedResultType() ) );
// TODO: when the QueryInterpretationCache can handle caching criteria queries,
// simply cache the new SQM as if it were a criteria query, and remove this:
getQueryOptions().setQueryPlanCachingEnabled( false );
setSqmStatement( selectStatement );
return this;
}
// @Override
// public SelectionQuery<R> addRestriction(Restriction<? super R> restriction) {
// final SqmSelectStatement<R> selectStatement = getSqmSelectStatement().copy( noParamCopyContext( SqmQuerySource.CRITERIA ) );
// restriction.apply( selectStatement, selectStatement.<R>getRoot( 0, getExpectedResultType() ) );
// // TODO: when the QueryInterpretationCache can handle caching criteria queries,
// // simply cache the new SQM as if it were a criteria query, and remove this:
// getQueryOptions().setQueryPlanCachingEnabled( false );
// setSqmStatement( selectStatement );
// return this;
// }

@Override
public SelectionQuery<R> setPage(Page page) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.hibernate.query.hql.spi.SqmQueryImplementor;
import org.hibernate.query.internal.DelegatingDomainQueryExecutionContext;
import org.hibernate.query.internal.ParameterMetadataImpl;
import org.hibernate.query.restriction.Restriction;
import org.hibernate.query.spi.DelegatingQueryOptions;
import org.hibernate.query.spi.DomainQueryExecutionContext;
import org.hibernate.query.spi.HqlInterpretation;
Expand Down Expand Up @@ -781,12 +780,6 @@ public Query<R> setOrder(Order<? super R> order) {
return this;
}

@Override
public Query<R> addRestriction(Restriction<? super R> restriction) {
super.addRestriction( restriction );
return this;
}

@Override
public Query<R> setOrder(List<? extends Order<? super R>> orders) {
super.setOrder(orders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,30 +865,25 @@ public Map<JpaCriteriaParameter<?>, SqmJpaCriteriaParameterWrapper<?>> getJpaCri

public static SqmSortSpecification sortSpecification(SqmSelectStatement<?> sqm, Order<?> order) {
final List<SqmSelectableNode<?>> items = sqm.getQuerySpec().getSelectClause().getSelectionItems();
final int element = order.getElement();
if ( element < 1) {
throw new IllegalQueryOperationException("Cannot order by element " + element
+ " (the first select item is element 1)");
}
if ( element > items.size() ) {
throw new IllegalQueryOperationException("Cannot order by element " + element
+ " (there are only " + items.size() + " select items)");
}
final SqmSelectableNode<?> selected = items.get( element-1 );
final SqmSelectableNode<?> selected = selectedNode( sqm, order ); // does validation by side effect!
return createSortSpecification( sqm, order, items, selected );
}

private static SqmSortSpecification createSortSpecification(
SqmSelectStatement<?> sqm, Order<?> order, List<SqmSelectableNode<?>> items, SqmSelectableNode<?> selected) {
final NodeBuilder builder = sqm.nodeBuilder();
if ( order.getEntityClass() == null ) {
// ordering by an element of the select list
return new SqmSortSpecification(
new SqmAliasedNodeRef( element, builder.getIntegerType(), builder ),
new SqmAliasedNodeRef( order.getElement(), builder.getIntegerType(), builder ),
order.getDirection(),
order.getNullPrecedence(),
order.isCaseInsensitive()
);
}
else {
// ordering by an attribute of the returned entity
if ( items.size() == 1) {
if ( items.size() <= 1) {
if ( selected instanceof SqmFrom<?, ?> root ) {
if ( !order.getEntityClass().isAssignableFrom( root.getJavaType() ) ) {
throw new IllegalQueryOperationException("Select item was of wrong entity type");
Expand All @@ -898,7 +893,10 @@ public static SqmSortSpecification sortSpecification(SqmSelectStatement<?> sqm,
while ( tokens.hasMoreTokens() ) {
path = path.get( tokens.nextToken() );
}
return builder.sort( path, order.getDirection(), order.getNullPrecedence(), order.isCaseInsensitive() );
return builder.sort( path,
order.getDirection(),
order.getNullPrecedence(),
order.isCaseInsensitive() );
}
else {
throw new IllegalQueryOperationException("Select item was not an entity type");
Expand All @@ -910,6 +908,32 @@ public static SqmSortSpecification sortSpecification(SqmSelectStatement<?> sqm,
}
}

private static SqmSelectableNode<?> selectedNode(SqmSelectStatement<?> sqm, Order<?> order) {
final int element = order.getElement();
if ( element < 1) {
throw new IllegalQueryOperationException("Cannot order by element " + element
+ " (the first select item is element 1)");
}
final var selectionItems = sqm.getQuerySpec().getSelectClause().getSelectionItems();
final int items = selectionItems.size();
if ( items == 0 && element == 1 ) {
if ( order.getEntityClass() == null || sqm.getQuerySpec().getRootList().size() > 1 ) {
throw new IllegalQueryOperationException("Cannot order by element " + element
+ " (there is no select list)");
}
else {
return sqm.getQuerySpec().getRootList().get(0);
}
}
else if ( element > items ) {
throw new IllegalQueryOperationException( "Cannot order by element " + element
+ " (there are only " + items + " select items)");
}
else {
return selectionItems.get( element - 1 );
}
}

public static boolean isSelectionAssignableToResultType(SqmSelection<?> selection, Class<?> expectedResultType) {
if ( expectedResultType == null ) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.hibernate.query.QueryParameter;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.restriction.Restriction;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.sqm.SqmSelectionQuery;
import org.hibernate.query.sqm.tree.SqmStatement;
Expand Down Expand Up @@ -310,12 +309,6 @@ public SqmSelectionQueryImplementor<R> setOrder(Order<? super R> order) {
return this;
}

@Override
public SqmSelectionQueryImplementor<R> addRestriction(Restriction<? super R> restriction) {
getDelegate().addRestriction( restriction );
return this;
}

@Override
public SqmSelectionQueryImplementor<R> setFollowOnLocking(boolean enable) {
getDelegate().setFollowOnLocking( enable );
Expand Down
Loading
Loading