HHH-16179 Session.find should not apply filters

This commit is contained in:
Christian Beikov 2023-03-02 13:10:38 +01:00
parent f5741e9b7e
commit 1ff682438d
7 changed files with 25 additions and 13 deletions

View File

@ -542,9 +542,9 @@ include::{extrasdir}/pc-filter-entity-query-example.sql[]
[IMPORTANT] [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]] [[pc-filter-entity-example]]
.Fetching entities mapped with `@Filter` .Fetching entities mapped with `@Filter`

View File

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

View File

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

View File

@ -67,7 +67,9 @@ public class FilterParameterTests {
scope.inTransaction( (session) -> { scope.inTransaction( (session) -> {
session.enableFilter( "filterYesNoConverter" ).setParameter( "yesNo", Boolean.FALSE ); 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(); assertThat( loaded ).isNull();
} ); } );
} }
@ -95,7 +97,9 @@ public class FilterParameterTests {
session.enableFilter( "filterYesNoBoolean" ).setParameter( "yesNo", Boolean.FALSE ); session.enableFilter( "filterYesNoBoolean" ).setParameter( "yesNo", Boolean.FALSE );
try { 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" ); fail( "Expecting an exception" );
} }
catch (Exception expected) { catch (Exception expected) {
@ -114,7 +118,9 @@ public class FilterParameterTests {
scope.inTransaction( (session) -> { scope.inTransaction( (session) -> {
session.enableFilter( "filterNumberConverter" ).setParameter( "zeroOne", Boolean.FALSE ); 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(); assertThat( loaded ).isNull();
} ); } );
} }
@ -142,7 +148,9 @@ public class FilterParameterTests {
session.enableFilter( "filterNumberBoolean" ).setParameter( "zeroOne", Boolean.FALSE ); session.enableFilter( "filterNumberBoolean" ).setParameter( "zeroOne", Boolean.FALSE );
try { 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" ); fail( "Expecting an exception" );
} }
catch (Exception expected) { catch (Exception expected) {
@ -166,7 +174,9 @@ public class FilterParameterTests {
session.enableFilter( "filterMismatchConverter" ).setParameter( "mismatch", Boolean.FALSE ); session.enableFilter( "filterMismatchConverter" ).setParameter( "mismatch", Boolean.FALSE );
try { 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" ); fail( "Expecting an exception" );
} }
catch (Exception expected) { catch (Exception expected) {

View File

@ -81,7 +81,7 @@ public class TenantIdTest implements SessionFactoryProducer {
currentTenant = "yours"; currentTenant = "yours";
scope.inTransaction( session -> { 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() ); assertEquals( 0, session.createQuery("from Account").getResultList().size() );
session.disableFilter(TenantIdBinder.FILTER_NAME); session.disableFilter(TenantIdBinder.FILTER_NAME);
assertNotNull( session.find(Account.class, acc.id) ); assertNotNull( session.find(Account.class, acc.id) );

View File

@ -79,7 +79,7 @@ public class TenantLongIdTest implements SessionFactoryProducer {
currentTenant = yours; currentTenant = yours;
scope.inTransaction( session -> { 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() ); assertEquals( 0, session.createQuery("from Account").getResultList().size() );
session.disableFilter(TenantIdBinder.FILTER_NAME); session.disableFilter(TenantIdBinder.FILTER_NAME);
assertNotNull( session.find(Account.class, acc.id) ); assertNotNull( session.find(Account.class, acc.id) );

View File

@ -81,7 +81,7 @@ public class TenantUuidTest implements SessionFactoryProducer {
currentTenant = yours; currentTenant = yours;
scope.inTransaction( session -> { 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() ); assertEquals( 0, session.createQuery("from Account").getResultList().size() );
session.disableFilter(TenantIdBinder.FILTER_NAME); session.disableFilter(TenantIdBinder.FILTER_NAME);
assertNotNull( session.find(Account.class, acc.id) ); assertNotNull( session.find(Account.class, acc.id) );