HHH-17305 Test and fix for keeping order of tables in polymorphic queries

This commit is contained in:
Christian Thiel 2023-10-23 11:39:32 +02:00 committed by Christian Beikov
parent 0c7c81cce7
commit e8426692e4
3 changed files with 21 additions and 33 deletions

View File

@ -40,8 +40,8 @@ public class QuerySplitter {
return sqmSelectStatement;
}
final SqmPolymorphicRootDescriptor<?> unmappedPolymorphicDescriptor = (SqmPolymorphicRootDescriptor<?>) unmappedPolymorphicReference.getReferencedPathSource();
final Set<EntityDomainType<?>> implementors = unmappedPolymorphicDescriptor.getImplementors();
final SqmPolymorphicRootDescriptor<R> unmappedPolymorphicDescriptor = (SqmPolymorphicRootDescriptor<R>) unmappedPolymorphicReference.getReferencedPathSource();
final Set<EntityDomainType<? extends R>> implementors = unmappedPolymorphicDescriptor.getImplementors();
@SuppressWarnings("unchecked")
final SqmSelectStatement<R>[] expanded = new SqmSelectStatement[ implementors.size() ];
@ -107,8 +107,8 @@ public class QuerySplitter {
return sqmDeleteStatement;
}
final SqmPolymorphicRootDescriptor<?> unmappedPolymorphicDescriptor = (SqmPolymorphicRootDescriptor<?>) unmappedPolymorphicReference.getReferencedPathSource();
final Set<EntityDomainType<?>> implementors = unmappedPolymorphicDescriptor.getImplementors();
final SqmPolymorphicRootDescriptor<R> unmappedPolymorphicDescriptor = (SqmPolymorphicRootDescriptor<R>) unmappedPolymorphicReference.getReferencedPathSource();
final Set<EntityDomainType<? extends R>> implementors = unmappedPolymorphicDescriptor.getImplementors();
@SuppressWarnings("unchecked")
final SqmDeleteStatement<R>[] expanded = new SqmDeleteStatement[ implementors.size() ];

View File

@ -6,36 +6,16 @@
*/
package org.hibernate.query.sqm.tree.domain;
import java.util.ArrayList;
import java.util.Collection;
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;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.CollectionAttribute;
import jakarta.persistence.metamodel.ListAttribute;
import jakarta.persistence.metamodel.MapAttribute;
import jakarta.persistence.metamodel.PluralAttribute;
import jakarta.persistence.metamodel.SetAttribute;
import jakarta.persistence.metamodel.SingularAttribute;
import jakarta.persistence.metamodel.*;
import org.hibernate.metamodel.RepresentationMode;
import org.hibernate.metamodel.model.domain.DomainType;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.metamodel.model.domain.IdentifiableDomainType;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
import org.hibernate.metamodel.model.domain.PluralPersistentAttribute;
import org.hibernate.metamodel.model.domain.SimpleDomainType;
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute;
import org.hibernate.metamodel.model.domain.*;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.type.descriptor.java.JavaType;
import java.util.*;
import java.util.stream.Collectors;
import java.util.function.Consumer;
import static java.util.Collections.unmodifiableMap;
/**
@ -53,7 +33,9 @@ public class SqmPolymorphicRootDescriptor<T> implements EntityDomainType<T> {
JavaType<T> polymorphicJavaType,
Set<EntityDomainType<? extends T>> implementors) {
this.polymorphicJavaType = polymorphicJavaType;
this.implementors = implementors;
TreeSet<EntityDomainType<? extends T>> treeSet = new TreeSet<>( Comparator.comparing(EntityDomainType::getTypeName) );
treeSet.addAll( implementors );
this.implementors = treeSet;
this.commonAttributes = unmodifiableMap( inferCommonAttributes( implementors ) );
}
@ -111,8 +93,8 @@ public class SqmPolymorphicRootDescriptor<T> implements EntityDomainType<T> {
return true;
}
public Set<EntityDomainType<?>> getImplementors() {
return new HashSet<>( implementors );
public Set<EntityDomainType<? extends T>> getImplementors() {
return implementors;
}
@Override

View File

@ -60,6 +60,8 @@ public class PolymorphicQueriesTest {
session -> {
List<I> results = session.createQuery( "from " + I.class.getName(), I.class ).list();
assertThat( results.size() ).isEqualTo( 2 );
assertThat(results.get(0)).isInstanceOf(EntityA.class);
assertThat(results.get(1)).isInstanceOf(EntityB.class);
}
);
@ -67,6 +69,8 @@ public class PolymorphicQueriesTest {
session -> {
List<I> results = session.createQuery( "from " + I.class.getName() + " i", I.class ).list();
assertThat( results.size() ).isEqualTo( 2 );
assertThat(results.get(0)).isInstanceOf(EntityA.class);
assertThat(results.get(1)).isInstanceOf(EntityB.class);
}
);
@ -75,6 +79,8 @@ public class PolymorphicQueriesTest {
List<I> results = session.createQuery( "select i from " + I.class.getName() + " i", I.class )
.list();
assertThat( results.size() ).isEqualTo( 2 );
assertThat(results.get(0)).isInstanceOf(EntityA.class);
assertThat(results.get(1)).isInstanceOf(EntityB.class);
}
);
}