Fix issue with query use subclass attribute in join

This commit is contained in:
Andrea Boriero 2020-07-02 10:04:03 +01:00
parent bfd15ec0e8
commit 55960167bc
4 changed files with 29 additions and 2 deletions

View File

@ -7,9 +7,11 @@
package org.hibernate.metamodel.model.domain;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
@ -45,6 +47,8 @@ public abstract class AbstractManagedType<J>
private final Map<String, SingularPersistentAttribute<J, ?>> declaredSingularAttributes = new HashMap<>();
private final Map<String, PluralPersistentAttribute<J, ?, ?>> declaredPluralAttributes = new HashMap<>();
private final List<ManagedDomainType> subTypes = new ArrayList<>();
protected AbstractManagedType(
String hibernateTypeName,
JavaTypeDescriptor<J> javaTypeDescriptor,
@ -53,6 +57,9 @@ public abstract class AbstractManagedType<J>
super( javaTypeDescriptor, domainMetamodel );
this.hibernateTypeName = hibernateTypeName;
this.superType = superType;
if ( superType != null ) {
superType.addSubType( this );
}
// todo (6.0) : need to handle RepresentationMode#MAP as well
this.representationMode = RepresentationMode.POJO;
@ -69,6 +76,11 @@ public abstract class AbstractManagedType<J>
return superType;
}
@Override
public void addSubType(ManagedDomainType subType){
subTypes.add( subType );
}
@Override
public RepresentationMode getRepresentationMode() {
return representationMode;
@ -138,6 +150,13 @@ public abstract class AbstractManagedType<J>
}
}
for ( ManagedDomainType subType : subTypes ) {
PersistentAttribute subTypeAttribute = subType.findAttribute( name );
if ( subTypeAttribute != null ) {
return subTypeAttribute;
}
}
return null;
}

View File

@ -39,6 +39,8 @@ public interface ManagedDomainType<J> extends SimpleDomainType<J>, ManagedType<J
*/
ManagedDomainType<? super J> getSuperType();
void addSubType(ManagedDomainType subType);
void visitAttributes(Consumer<PersistentAttribute<J,?>> action);
void visitDeclaredAttributes(Consumer<PersistentAttribute<J,?>> action);

View File

@ -34,12 +34,13 @@ public class EntitySqmPathSource<J> extends AbstractSqmPathSource<J> {
@Override
public SqmPathSource<?> findSubPathSource(String name) {
final PersistentAttribute<?,?> attribute = getSqmPathType().findAttribute( name );
final EntityDomainType<J> sqmPathType = getSqmPathType();
final PersistentAttribute<?,?> attribute = sqmPathType.findAttribute( name );
if ( attribute != null ) {
return (SqmPathSource<?>) attribute;
}
final SingularPersistentAttribute<J, ?> idAttribute = getSqmPathType().findIdAttribute();
final SingularPersistentAttribute<J, ?> idAttribute = sqmPathType.findIdAttribute();
if ( idAttribute != null && idAttribute.getName().equals( name ) ) {
return idAttribute;
}

View File

@ -463,4 +463,9 @@ public class SqmPolymorphicRootDescriptor<T> implements EntityDomainType<T> {
public ManagedDomainType<? super T> getSuperType() {
throw new UnsupportedOperationException( );
}
@Override
public void addSubType(ManagedDomainType subType) {
throw new UnsupportedOperationException( );
}
}