简体   繁体   中英

Disable filter in the hibernate session

I have created a filter to apply for session of a particular bean. I enable the filter each time the session is fetched:

@Override
protected final Session getHibernateSession() {
   enableFilter("filterName",
         "parameterName", "parameterValue");
   return super.getHibernateSession();
}

Now I want that for a particular query, this filter need not be enabled. There exists an option to disable the filter, but I am not able to find the right place to do so. I tried:

DetachedCriteria criteria = criteria();
// some restrictions and projections
disableFilter("filterName")
return find(criteria);

The problem is, when the find(criteria) is fired, the hibernate session is fetched again, which enables the filter.

I think you have created separate methods for enabling and disabling filters.

You will have to disable the filters in find(criteria) method itself. Instead of doing :

getHibernateSession().createQuery(somequery);

You will have to do something like this :

Session session = getHibernateSession();
session.disableFilter("filterName");
session.createQuery(somequery);

This should work. I also found a link which can help: https://www.tikalk.com/posts/2013/01/28/how-to-activate-and-deactivate-hibernate-filters/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM