diff --git a/documentation/src/main/asciidoc/userguide/chapters/pc/PersistenceContext.adoc b/documentation/src/main/asciidoc/userguide/chapters/pc/PersistenceContext.adoc index c0d01f8fda..76cc434f98 100644 --- a/documentation/src/main/asciidoc/userguide/chapters/pc/PersistenceContext.adoc +++ b/documentation/src/main/asciidoc/userguide/chapters/pc/PersistenceContext.adoc @@ -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` diff --git a/documentation/src/test/java/org/hibernate/userguide/pc/FilterTest.java b/documentation/src/test/java/org/hibernate/userguide/pc/FilterTest.java index aacc18ff91..98eef2dc51 100644 --- a/documentation/src/test/java/org/hibernate/userguide/pc/FilterTest.java +++ b/documentation/src/test/java/org/hibernate/userguide/pc/FilterTest.java @@ -110,7 +110,7 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase { Account account2 = entityManager.find(Account.class, 2L); assertNotNull(account1); - assertNull(account2); + assertNotNull(account2); }); doInJPA(this::entityManagerFactory, entityManager -> { @@ -146,7 +146,7 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase { Account account = entityManager.find(Account.class, 2L); - assertNull(account); + assertFalse( account.isActive() ); //end::pc-filter-entity-example[] }); diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java index e2a3dbd024..4985d73407 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java @@ -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 @@ public class LoaderSelectBuilder { querySpec::applyPredicate, tableGroup, true, - loadQueryInfluencers.getEnabledFilters(), + // HHH-16179 Session.find should not apply filters + Collections.emptyMap(),//loadQueryInfluencers.getEnabledFilters(), null, astCreationState ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/filter/FilterParameterTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/filter/FilterParameterTests.java index b4938afe4c..e3542e3e5a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/filter/FilterParameterTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/filter/FilterParameterTests.java @@ -67,7 +67,9 @@ public class FilterParameterTests { 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 class FilterParameterTests { 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 class FilterParameterTests { 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 class FilterParameterTests { 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 class FilterParameterTests { 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) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/tenantid/TenantIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/tenantid/TenantIdTest.java index 832f4537f3..7ecf44e2ee 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/tenantid/TenantIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/tenantid/TenantIdTest.java @@ -81,7 +81,7 @@ public class TenantIdTest implements SessionFactoryProducer { 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) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/tenantlongid/TenantLongIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/tenantlongid/TenantLongIdTest.java index e14f1b8d9d..300d4b9676 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/tenantlongid/TenantLongIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/tenantlongid/TenantLongIdTest.java @@ -79,7 +79,7 @@ public class TenantLongIdTest implements SessionFactoryProducer { 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) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/tenantuuid/TenantUuidTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/tenantuuid/TenantUuidTest.java index ec0f1bf67f..26606ba401 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/tenantuuid/TenantUuidTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/tenantuuid/TenantUuidTest.java @@ -81,7 +81,7 @@ public class TenantUuidTest implements SessionFactoryProducer { 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) );