Fix some documentation tests and respect filters in Session.find as per HHH-14772, and respect explicit polymorphism

This commit is contained in:
Christian Beikov 2021-08-06 18:05:30 +02:00
parent b6683d2352
commit 2f166c31df
6 changed files with 22 additions and 24 deletions

View File

@ -540,9 +540,9 @@ include::{extrasdir}/pc-filter-entity-query-example.sql[]
[IMPORTANT]
====
Filters apply to entity queries, but not to direct fetching.
Filters apply to entity queries and to direct fetching.
Therefore, in the following example, the filter is not taken into consideration when fetching an entity from the Persistence Context.
Therefore, even in the following example, the filter is taken into consideration when fetching an entity from the Persistence Context.
[[pc-filter-entity-example]]
.Fetching entities mapped with `@Filter`

View File

@ -326,7 +326,7 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
List<Phone> phones = entityManager.createQuery( criteria ).getResultList();
//end::criteria-from-join-example[]
assertEquals(2, phones.size());
assertEquals( 1, phones.size() );
});
}
@ -349,7 +349,7 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
List<Phone> phones = entityManager.createQuery( criteria ).getResultList();
//end::criteria-from-fetch-example[]
assertEquals(2, phones.size());
assertEquals( 1, phones.size() );
});
}

View File

@ -112,7 +112,8 @@ public class EntityTypeChangeAuditTest extends BaseEntityManagerFunctionalTestCa
AuditReaderFactory
.get( entityManager )
.getCrossTypeRevisionChangesReader()
.findEntityTypes( 2 )
// 52 is the next id on the DB due to allocationSize defaulting to 50
.findEntityTypes( 52 )
.iterator().next()
.getFirst()
);

View File

@ -24,14 +24,10 @@ import org.hibernate.Session;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
import org.hibernate.annotations.SqlFragmentAlias;
import org.hibernate.annotations.Where;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import org.jboss.logging.Logger;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -104,7 +100,7 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
Account account2 = entityManager.find( Account.class, 2L );
assertNotNull( account1 );
assertNotNull( account2 );
assertNull( account2 );
} );
doInJPA( this::entityManagerFactory, entityManager -> {
@ -140,7 +136,7 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
Account account = entityManager.find( Account.class, 2L );
assertFalse( account.isActive() );
assertNull( account );
//end::pc-filter-entity-example[]
} );
@ -189,7 +185,7 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
Client client = entityManager.find( Client.class, 1L );
assertEquals( 1, client.getAccounts().size() );
assertEquals( 2, client.getAccounts().size() );
//end::pc-filter-collection-query-example[]
} );
}
@ -217,11 +213,7 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
)
@Filter(
name="activeAccount",
condition="{a}.active_status = :active and {a}.type = {c}.type",
aliases = {
@SqlFragmentAlias( alias = "a", table= "account"),
@SqlFragmentAlias( alias = "c", table= "client"),
}
condition="{a}.active_status = :active"
)
private List<Account> accounts = new ArrayList<>( );

View File

@ -16,7 +16,6 @@ import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.collection.spi.BagSemantics;
import org.hibernate.engine.FetchStyle;
@ -598,10 +597,10 @@ public class LoaderSelectBuilder {
QuerySpec querySpec,
TableGroup tableGroup,
PluralAttributeMapping pluralAttributeMapping) {
final Joinable joinable = pluralAttributeMapping
.getCollectionDescriptor()
.getCollectionType()
.getAssociatedJoinable( creationContext.getSessionFactory() );
final CollectionPersister collectionPersister = pluralAttributeMapping.getCollectionDescriptor();
final Joinable joinable = collectionPersister
.getCollectionType()
.getAssociatedJoinable( creationContext.getSessionFactory() );
final Predicate filterPredicate = FilterHelper.createFilterPredicate(
loadQueryInfluencers,
joinable,
@ -610,7 +609,7 @@ public class LoaderSelectBuilder {
if ( filterPredicate != null ) {
querySpec.applyPredicate( filterPredicate );
}
if ( pluralAttributeMapping.getCollectionDescriptor().isManyToMany() ) {
if ( collectionPersister.isManyToMany() ) {
assert joinable instanceof CollectionPersister;
final Predicate manyToManyFilterPredicate = FilterHelper.createManyToManyFilterPredicate(
loadQueryInfluencers,

View File

@ -49,6 +49,7 @@ import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.MappedSuperclassDomainType;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.persister.entity.Queryable;
import org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.spi.DynamicModelJtd;
@ -460,7 +461,12 @@ public class JpaMetamodelImpl implements JpaMetamodel {
visitEntityTypes(
entityDomainType -> {
if ( javaType.isAssignableFrom( entityDomainType.getJavaType() ) ) {
matchingDescriptors.add( entityDomainType );
final Queryable entityPersister = (Queryable) typeConfiguration.getSessionFactory()
.getMetamodel()
.getEntityDescriptor( entityDomainType.getHibernateEntityName() );
if ( !entityPersister.isExplicitPolymorphism() ) {
matchingDescriptors.add( entityDomainType );
}
}
}
);