support for selecting of joined plural attributes

This commit is contained in:
Steve Ebersole 2021-04-27 11:03:10 -05:00
parent 19fbf18908
commit 9348952507
3 changed files with 53 additions and 1 deletions

View File

@ -41,6 +41,7 @@ import org.hibernate.grammars.hql.HqlParser;
import org.hibernate.grammars.hql.HqlParserBaseVisitor;
import org.hibernate.internal.util.collections.Stack;
import org.hibernate.internal.util.collections.StandardStack;
import org.hibernate.jpa.spi.JpaCompliance;
import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.metamodel.model.domain.AllowableFunctionReturnType;
import org.hibernate.metamodel.model.domain.BasicDomainType;
@ -825,7 +826,29 @@ public class SemanticQueryBuilder<R> extends HqlParserBaseVisitor<Object> implem
}
if ( ctx.selectExpression().expression() != null ) {
return (SqmExpression) ctx.selectExpression().expression().accept( this );
final SqmExpression<?> sqmExpression = (SqmExpression<?>) ctx.selectExpression().expression().accept( this );
if ( sqmExpression instanceof SqmPath ) {
final SqmPath<?> sqmPath = (SqmPath<?>) sqmExpression;
if ( sqmPath.getReferencedPathSource() instanceof PluralPersistentAttribute ) {
// for plural-attribute selections, use the element path as the selection
// - this is not strictly JPA compliant
if ( creationOptions.useStrictJpaCompliance() ) {
SqmTreeCreationLogger.LOGGER.debugf(
"Raw selection of plural attribute not supported by JPA: %s. Use `value(%s)` or `key(%s)` to indicate what part of the collection to select",
sqmPath.getAlias(),
sqmPath.getAlias(),
sqmPath.getAlias()
);
}
final PluralPersistentAttribute<?,?,?> pluralAttribute = (PluralPersistentAttribute<?,?,?>) sqmPath.getReferencedPathSource();
final SqmPath<?> elementPath = pluralAttribute.getElementPathSource().createSqmPath( sqmPath );
processingStateStack.getCurrent().getPathRegistry().register( elementPath );
return elementPath;
}
}
return sqmExpression;
}
throw new ParsingException( "Unexpected selection rule type : " + ctx.getText() );

View File

@ -6,11 +6,21 @@
*/
package org.hibernate.sql.ast.tree.predicate;
import org.hibernate.sql.ast.spi.SqlAstTreeHelper;
import org.hibernate.sql.ast.tree.SqlAstNode;
/**
* Models a predicate in the SQL AST
*
* @author Steve Ebersole
*/
public interface Predicate extends SqlAstNode {
/**
* Short-cut for {@link SqlAstTreeHelper#combinePredicates}
*/
static Predicate combinePredicates(Predicate p1, Predicate p2) {
return SqlAstTreeHelper.combinePredicates( p1, p2 );
}
boolean isEmpty();
}

View File

@ -214,6 +214,25 @@ public class SelectClauseTests extends BaseSqmUnitTest {
}
@Test
public void testPluralJoinSelection() {
collectionValueFunctionAssertions(
interpretSelect( "select l from EntityOfLists e join e.listOfBasics l" ),
EntityOfLists.class.getName() + ".listOfBasics",
"l"
);
collectionValueFunctionAssertions(
interpretSelect( "select l from EntityOfLists e join e.listOfComponents l" ),
EntityOfLists.class.getName() + ".listOfComponents",
"l"
);
collectionValueFunctionAssertions(
interpretSelect( "select l from EntityOfLists e join e.listOfOneToMany l" ),
EntityOfLists.class.getName() + ".listOfOneToMany",
"l"
);
}
@Test
public void testCollectionValueFunction() {
collectionValueFunctionAssertions(