Skip to content

Commit

Permalink
HHH-17305 Test and fix for keeping order of tables in polymorphic que…
Browse files Browse the repository at this point in the history
…ries
  • Loading branch information
Christian Thiel authored and beikov committed May 6, 2024
1 parent 0c7c81c commit e842669
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static <R> SqmSelectStatement<R>[] split(SqmSelectStatement<R> statement)
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() ];

Expand Down Expand Up @@ -107,8 +107,8 @@ public static <R> SqmDeleteStatement<R>[] split(SqmDeleteStatement<R> statement)
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() ];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -53,7 +33,9 @@ public SqmPolymorphicRootDescriptor(
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 ) );
}

Expand Down Expand Up @@ -111,8 +93,8 @@ private static boolean isACommonAttribute(List<EntityDomainType<?>> subList, Per
return true;
}

public Set<EntityDomainType<?>> getImplementors() {
return new HashSet<>( implementors );
public Set<EntityDomainType<? extends T>> getImplementors() {
return implementors;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ public void testQuery(SessionFactoryScope scope) {
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);
}
);

scope.inTransaction(
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);
}
);

Expand All @@ -75,6 +79,8 @@ public void testQuery(SessionFactoryScope scope) {
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);
}
);
}
Expand Down

0 comments on commit e842669

Please sign in to comment.