Skip to content

FilterDef and Filter annotations not working after upgrade. #2565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
achris001c opened this issue Jun 8, 2022 · 7 comments
Open

FilterDef and Filter annotations not working after upgrade. #2565

achris001c opened this issue Jun 8, 2022 · 7 comments
Labels
status: feedback-provided Feedback has been provided

Comments

@achris001c
Copy link

achris001c commented Jun 8, 2022

Previous Dependency Versions:

  • Spring boot v2.1.2, Spring data JPA v2.1.4 , Hibernate 5.3.7.Final

Upgraded Dependency Versions:

  • Spring boot v2.5.12, spring data jpa v2.5.10, Hibernate v5.4.33

Query Generation with earlier version (Repository.getById()):
Hibernate: select useraccoun0_.id as id1_2_, useraccoun0_.tenant_id as tenant_i4_2_, useraccoun0_.first_name as first_na2_2_, useraccoun0_.last_name as last_nam3_2_ from user_account useraccoun0_ where useraccoun0_.tenant_id = ? and useraccoun0_.id=?

Query Generation after upgrade (Repository.getById()):
Hibernate: select useraccoun0_.id as id1_2_0_, useraccoun0_.tenant_id as tenant_i4_2_0_, useraccoun0_.first_name as first_na2_2_0_, useraccoun0_.last_name as last_nam3_2_0_ from user_account useraccoun0_ where useraccoun0_.id=?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 8, 2022
@achris001c achris001c changed the title FilterDef and Filter annotations are not working after upgrading to spring-boot-starter-data-jpa:jar:2.5.12(spring-data-jpa:jar:2.5.10,hibernate-core:jar:5.4.33) FilterDef and Filter annotations not working after upgrade. Jun 8, 2022
@schauder
Copy link
Contributor

schauder commented Jun 8, 2022

Please provide a Minimimal Reproducable Example, preferable as a Github repository. Make sure to include the database, either as an in memory database or if that is not possible using Testcontainers.

If you can also provide a demonstration that whatever you try to do works with plain JPA we'd appreciate it.

@schauder schauder added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 8, 2022
@achris001c
Copy link
Author

Filters Working Code Base : https://github.com/achris001c/MultiTenantService/tree/working

Code Base where Filters don't work : https://github.com/achris001c/MultiTenantService/tree/not-working

Dependencies on "working" branch: Spring boot v2.1.2, Spring data JPA v2.1.4 , Hibernate 5.3.7.Final
Dependencies on "not-working" branch : Spring boot v2.5.12, spring data jpa v2.5.10, Hibernate v5.4.33

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 8, 2022
@derekm
Copy link

derekm commented Aug 27, 2024

@achris001c -- Was this ever fixed? Are FilterDefs no longer supported?

@derekm
Copy link

derekm commented Aug 27, 2024

@schauder -- The reporter of this issue closed their own issue after 2 days, and this loss of functionality was never addressed by the project. Closing this issue appears to have been a mistake. Their "minimally reproducible example" shows this not working all the way up to 2.7.

Someone else has gone out of their way to restore this functionality for themselves: https://babystepstopro.medium.com/adding-hibernate-entity-level-filtering-feature-to-spring-data-jpa-repository-76bf8ddd42ff

Somewhere between Spring Boot 2.1 and 2.5, Spring stopped creating JpaRepository queries through EntityManager?

Can this functionality be brought back in Spring Boot 3.x? We can unwrap our own Sessions and enable our own filters in our own Service layers. Spring Boot has a hard dependency on Hibernate, so why not restore this feature?

@schauder
Copy link
Contributor

I'm reopening this so someone can take a look what is going on.

Somewhere between Spring Boot 2.1 and 2.5, Spring stopped creating JpaRepository queries through EntityManager?

All queries executed by Spring Data JPA go through the EntityManager, so that's most certainly not the reason. I currently have no idea what is.

@schauder schauder reopened this Aug 28, 2024
@zachary-wang12
Copy link

zachary-wang12 commented Nov 1, 2024

@schauder

I'm using the latest spring data jpa, and still met the same issue.

As my experience, the transaction must be enabled so that the filter could be applied. I don't know if it is the behavior from spring data jpa or hibernate.

In hibernate document it uses the code below to enable filter

entityManager
    .unwrap(Session.class)
    .enableFilter("activeAccount") //This line enables the filter
    .setParameter("active", true);

List<Account> accounts = entityManager.createQuery(
    "select a from Account a", Account.class)
.getResultList();

assertEquals(2, accounts.size());

But I don't see any related code to use transaction.

I'm not the expert so could anyone determine the behavior if filter usage must comes with transaction is determined by hibernate or spring data jpa?

@marcioggs
Copy link

marcioggs commented Feb 12, 2025

I'm using Hibernate 6.5.5 and had to add the autoEnabled parameter after the upgrade for it to work like before on Spring Data JPA.

@FilterDef(name = "FILTER_NAME", autoEnabled = true)

I didn't find any related instruction in the Hibernate migration guides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided
Projects
None yet
Development

No branches or pull requests

6 participants