Skip to content

Commit

Permalink
HHH-19116 Return actual path source for singular attribute joins
Browse files Browse the repository at this point in the history
This fixes the check for `fk()` function argument type
  • Loading branch information
mbladel committed Feb 19, 2025
1 parent 88c9758 commit b788d42
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;

import jakarta.persistence.metamodel.Bindable;
import org.hibernate.Incubating;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.UnsupportedMappingException;
Expand All @@ -31,6 +32,7 @@
import org.hibernate.query.sqm.tree.select.SqmSubQuery;
import org.hibernate.sql.ast.spi.FromClauseAccess;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.type.BasicType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.ObjectArrayJavaType;

Expand Down Expand Up @@ -177,20 +179,21 @@ public SqmPathSource<?> findSubPathSource(String name) {
final SqmSelectableNode<?> component = components[index];
if ( component instanceof SqmPath<?> ) {
final SqmPath<?> sqmPath = (SqmPath<?>) component;
if ( sqmPath.getNodeType() instanceof SingularPersistentAttribute<?, ?> ) {
final Bindable<?> model = sqmPath.getModel();
if ( model instanceof SingularPersistentAttribute<?, ?> ) {
//noinspection unchecked,rawtypes
return new AnonymousTupleSqmAssociationPathSource(
name,
sqmPath,
( (SingularPersistentAttribute<?, ?>) sqmPath.getNodeType() ).getType()
( (SingularPersistentAttribute<?, ?>) model ).getType()
);
}
else if ( sqmPath.getNodeType() instanceof PluralPersistentAttribute<?, ?, ?> ) {
else if ( model instanceof PluralPersistentAttribute<?, ?, ?> ) {
//noinspection unchecked,rawtypes
return new AnonymousTupleSqmAssociationPathSource(
name,
sqmPath,
( (PluralPersistentAttribute<?, ?, ?>) sqmPath.getNodeType() ).getElementType()
( (PluralPersistentAttribute<?, ?, ?>) model ).getElementType()
);
}
else if ( sqmPath.getNodeType() instanceof EntityDomainType<?> ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private AttributeJoinDelegate resolveAlias(String identifier, boolean isTerminal
if ( allowReuse ) {
if ( !isTerminal ) {
for ( SqmJoin<?, ?> sqmJoin : lhs.getSqmJoins() ) {
if ( sqmJoin.getAlias() == null && sqmJoin.getReferencedPathSource() == subPathSource ) {
if ( sqmJoin.getAlias() == null && sqmJoin.getModel() == subPathSource ) {
return sqmJoin;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public static <T, A> SqmAttributeJoin<T, A> findCompatibleFetchJoin(
SqmPathSource<A> pathSource,
SqmJoinType requestedJoinType) {
for ( final SqmJoin<T, ?> join : sqmFrom.getSqmJoins() ) {
if ( join.getReferencedPathSource() == pathSource ) {
if ( join.getModel() == pathSource ) {
final SqmAttributeJoin<T, ?> attributeJoin = (SqmAttributeJoin<T, ?>) join;
if ( attributeJoin.isFetched() ) {
final SqmJoinType joinType = join.getSqmJoinType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public <X> X accept(SemanticQueryWalker<X> walker) {
@Override
public PersistentAttribute<? super O, ?> getAttribute() {
//noinspection unchecked
return (PersistentAttribute<? super O, ?>) getReferencedPathSource();
return (PersistentAttribute<? super O, ?>) getModel();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute;
import org.hibernate.metamodel.model.domain.TreatableDomainType;
import org.hibernate.query.sqm.SemanticQueryWalker;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.spi.NavigablePath;
import org.hibernate.query.hql.spi.SqmCreationProcessingState;
import org.hibernate.query.sqm.NodeBuilder;
Expand Down Expand Up @@ -86,6 +87,16 @@ public SqmSingularJoin<O, T> copy(SqmCopyContext context) {
return path;
}

@Override
public SqmPathSource<T> getNodeType() {
return getReferencedPathSource();
}

@Override
public SqmPathSource<T> getReferencedPathSource() {
return getModel().getPathSource();
}

@Override
public SingularPersistentAttribute<O, T> getModel() {
return (SingularPersistentAttribute<O, T>) super.getNodeType();
Expand Down

0 comments on commit b788d42

Please sign in to comment.