HHH-16179 Session.find should not apply filters

This commit is contained in:
Christian Beikov 2023-03-02 13:10:38 +01:00
parent 2c9a508dd2
commit 0b5e27d133
7 changed files with 25 additions and 13 deletions

View File

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

View File

@ -110,7 +110,7 @@ public void testLifecycle() {
Account account2 = entityManager.find(Account.class, 2L);
assertNotNull(account1);
assertNull(account2);
assertNotNull(account2);
});
doInJPA(this::entityManagerFactory, entityManager -> {
@ -146,7 +146,7 @@ public void testLifecycle() {
Account account = entityManager.find(Account.class, 2L);
assertNull(account);
assertFalse( account.isActive() );
//end::pc-filter-entity-example[]
});

View File

@ -7,6 +7,7 @@
package org.hibernate.loader.ast.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -625,7 +626,8 @@ private void applyFiltering(
querySpec::applyPredicate,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
// HHH-16179 Session.find should not apply filters
Collections.emptyMap(),//loadQueryInfluencers.getEnabledFilters(),
null,
astCreationState
);

View File

@ -67,7 +67,9 @@ public void testYesNo(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.enableFilter( "filterYesNoConverter" ).setParameter( "yesNo", Boolean.FALSE );
final EntityOne loaded = session.byId( EntityOne.class ).load( 1 );
final EntityOne loaded = session.createQuery( "from EntityOne e where e.id = :id", EntityOne.class )
.setParameter( "id", 1 )
.getSingleResultOrNull();
assertThat( loaded ).isNull();
} );
}
@ -95,7 +97,9 @@ public void testYesNoMismatch(SessionFactoryScope scope) {
session.enableFilter( "filterYesNoBoolean" ).setParameter( "yesNo", Boolean.FALSE );
try {
session.byId( EntityOne.class ).load( 1 );
session.createQuery( "from EntityOne e where e.id = :id", EntityOne.class )
.setParameter( "id", 1 )
.getSingleResultOrNull();
fail( "Expecting an exception" );
}
catch (Exception expected) {
@ -114,7 +118,9 @@ public void testNumeric(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.enableFilter( "filterNumberConverter" ).setParameter( "zeroOne", Boolean.FALSE );
final EntityTwo loaded = session.byId( EntityTwo.class ).load( 1 );
final EntityTwo loaded = session.createQuery( "from EntityTwo e where e.id = :id", EntityTwo.class )
.setParameter( "id", 1 )
.getSingleResultOrNull();
assertThat( loaded ).isNull();
} );
}
@ -142,7 +148,9 @@ public void testNumericMismatch(SessionFactoryScope scope) {
session.enableFilter( "filterNumberBoolean" ).setParameter( "zeroOne", Boolean.FALSE );
try {
session.byId( EntityTwo.class ).load( 1 );
session.createQuery( "from EntityTwo e where e.id = :id", EntityTwo.class )
.setParameter( "id", 1 )
.getSingleResultOrNull();
fail( "Expecting an exception" );
}
catch (Exception expected) {
@ -166,7 +174,9 @@ public void testMismatch(SessionFactoryScope scope) {
session.enableFilter( "filterMismatchConverter" ).setParameter( "mismatch", Boolean.FALSE );
try {
session.byId( EntityThree.class ).load( 1 );
session.createQuery( "from EntityThree e where e.id = :id", EntityThree.class )
.setParameter( "id", 1 )
.getSingleResultOrNull();
fail( "Expecting an exception" );
}
catch (Exception expected) {

View File

@ -81,7 +81,7 @@ public void test(SessionFactoryScope scope) {
currentTenant = "yours";
scope.inTransaction( session -> {
assertNull( session.find(Account.class, acc.id) );
assertNotNull( session.find(Account.class, acc.id) );
assertEquals( 0, session.createQuery("from Account").getResultList().size() );
session.disableFilter(TenantIdBinder.FILTER_NAME);
assertNotNull( session.find(Account.class, acc.id) );

View File

@ -79,7 +79,7 @@ public void test(SessionFactoryScope scope) {
currentTenant = yours;
scope.inTransaction( session -> {
assertNull( session.find(Account.class, acc.id) );
assertNotNull( session.find(Account.class, acc.id) );
assertEquals( 0, session.createQuery("from Account").getResultList().size() );
session.disableFilter(TenantIdBinder.FILTER_NAME);
assertNotNull( session.find(Account.class, acc.id) );

View File

@ -81,7 +81,7 @@ public void test(SessionFactoryScope scope) {
currentTenant = yours;
scope.inTransaction( session -> {
assertNull( session.find(Account.class, acc.id) );
assertNotNull( session.find(Account.class, acc.id) );
assertEquals( 0, session.createQuery("from Account").getResultList().size() );
session.disableFilter(TenantIdBinder.FILTER_NAME);
assertNotNull( session.find(Account.class, acc.id) );