fabien7474
fabien7474

Reputation: 16570

How to always enable my Hibernate filters in a spring application?

Having a Hibernate (3.5)/Spring (3.0)/BlazeDS/Flex stack-based application, I need to apply filters for some of my domain classes as shown below.

@FilterDef(name="notDeletedFilter")
@Filter(name="notDeletedFilter", condition="deleted=0")
public class Item {
   private boolean deleted;
  //setter and getter
}

These filters should always be applied in my application. However, according to the hibernate documentation, by default, filters are not enabled for a given hibernate session.

So my question is very simple: How can I enable all defined hibernate filters as above for all Hibernate sessions? Is there anyway to configure my Hibernate Session factory in a spring xml configuration file in order to apply these filters?

Upvotes: 5

Views: 3793

Answers (3)

InspectorGadget
InspectorGadget

Reputation: 1010

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.

Upvotes: 1

Arun R
Arun R

Reputation: 8392

You could use AOP (aspect oriented programming) to configure the filter.

Upvotes: 0

Brent Worden
Brent Worden

Reputation: 10994

If you are using Spring's HibernateTemplate, one solution is to extend it and override the enableFilters method. In it, explicitly enable the filters you need.

Upvotes: 2

Related Questions