views:

177

answers:

1

Good day,

I currently have an Entity that has a where clause set on it. I want to put that where clause on a filter and have that turned-on by default (so that I won't break any existing functionalities).

I want to turn it into a filter so that I can disable it because I have a use case wherein I need it disabled.

How can I do that in hibernate 3.1.3?

Thanks, Franz

A: 

The Hibernate documentation seems to state that Filters (akin to a SQL view) are optional ways of viewing your data and do not represent the "default" way of looking at it. My recommendation would be to stick with the where clause that you have and write a special accessor routine for the use case where you don't want filtered data.

I suppose another alternative to achieve what you're asking would be to encapsulate the enabling of your filter within your "HibernateUtil" class (assuming you have such a beast that is responsible for opening new sessions for your code to use). By default your method to obtain a new session would instantiate the session and then enable the filter before returning. Something like this:

public Session newSession(){
    Session session = sessionFactory.openSession();
    // this assumes you don't have criteria to set depending on the context
    session.enableFilter("yourFilter");  
    return session;
}

Then in your specific use case you could disableFilter("yourFilter");.

BryanD