0

I defined a Hibernate Filter to filter out the deleted records.

My wishlist:

The Filter should be enabled all the time as default. Only in special cases when I need the deleted records, then I would active disable the filter and enable it again.

@Filter(name = "deletedFilter", condition = "deleted = false")
@FilterDef(name = "deletedFilter")

Would be happy for some info how I can archive that.

br

1 Answer 1

0

As of Hibernate 6.5, autoEnabled parameter on the @FilterDef can be set to true which enables filter on every session by default. Refer to the documentation.

Additionally for spring, JpaVendorAdapter.postProcessEntityManager or setEntityManagerInitializer(Consumer<EntityManager>) method on JpaTransactionManager as well as LocalContainerEntityManagerFactoryBean can be used. Refer to this issue for more information.

As for disabling the filter when needed, you can call disableFilter method of entityManager, this way you can disable the filter for the current session.

Sign up to request clarification or add additional context in comments.

2 Comments

This sounds good, but I have problems to get the filter working, weither I do autoEnabled or enable it manual in the session, the filter dosn't work, the generated sql havn't attached the filter condition. Do you have any idea?
how are you using the queries, are you writing native query or using methods like findById or writing jpql? You didn't tell that in your question, you should edit the question and attach your code samples or ask another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.