Grails Hibernate Filter Plugin a Life Saver

13 / Jul / 2010 by Uday Pratap Singh 5 comments

The project I am currently working on is also accessible from the iPhone. In the last few months so many changes have been done on the web application (developed in groovy/grails). Now the client wants the same thing on iPhone as well. He has a separate development team for iPhone application. The iPhone application developer wanted to have soft delete mechanism for deleting an object. So they added new field in the database table named “deleted”.

Now we have to remove all such records from our every query in the project which have deleted true. So there were 6-7 domains which now have this mechanism of soft deletion.

That’s how our life became hell, we were unsure about what to do next, is it good to modify all the queries in the project which would be a lot of work.

I was also reading Hibernate books these days and found a very good thing in Hibernate about filters. So now I just need to look for how the filters work in Grails. In few seconds I found my “life saver” plugin for Grails i.e. http://www.grails.org/plugin/hibernate-filter. Everything is written in the plugin documentation but I would like to re-quote some of the documentation

My class was like

Class A{
String name
String address
Boolean deleted
} 

After installing the plugin I just need to add a line in the domain and a property in datasource file and it will filter all the data with the condition that I put in filter. So my domain would become like this.

Class A{
String name
String address
Boolean deleted
} 

static hibernateFilters = {
        enabledFilter(condition: 'deleted=0', default: true)
    }


in Datasource.groovy file I need to write

import  org.grails.plugin.hibernate.filter.HibernateFilterDomainConfiguration
dataSource {
   …
   configClass = HibernateFilterDomainConfiguration.class
}


and I am done without looking at how many places I have used dynamic finders or queried the database for this domain.

I haven’t gone into the details of the plugin but it really saved a lot of work for me.
Hope it helps

FOUND THIS USEFUL? SHARE IT

comments (5)

  1. Claes Svensson

    Oh, then I’m not really sure. I’ve only used it in combination with the FilterPane plugin, so it is probably like Uday says then. Good luck no matter how it works :-)

    Reply
  2. Uday

    In my case It was not giving all A’s and that what I wanted. I was using criteria queries to fetch the result but I dont think it really makes any difference. May be the plugin wouldn’t work if we fetch results from sql queries because in that case we are using sql package and not the Hibernate though I am not too sure

    Reply
  3. Lucas Teixeira

    Great to know. I’m using the multi-tenant plugin in other project and works like a charm :)
    This one have just some aspects of a multi tenant app, that’s why the hibernate-filters looked more interesting…

    I’ll try the criteria condition solution…

    Thanks Claes!

    Reply
  4. Claes Svensson

    I think it will give you all A:s which I wouldn’t say is a bad thing, sometimes you want all A:s and sometimes not. However, I’m using a condition like this for such a use-case:

    condition: ‘b_id in (select b.id from b where b.deleted = 0′)

    There is also a falcone & multi-tenant plugin that handles this scenario, which I’m tempted to try out sometime.

    Reply
  5. Lucas Teixeira

    Excellent!

    I’ll need this in some app here that works with some “multi instances” behaviour, and this will save me :)

    Just like to know one thing, the hibernate filter intercepts queries made just on the object youve defined like above, or if I query some other object (B for example), that have some relationship with A, it’ll be filtered either?

    For Example

    class A {
    B other
    Inter qtty
    }

    B {
    Boolean deleted
    (hibernate-filters definition like in your example)
    }

    If I query for all B instances, the plugin will intercept and I’ll have just the non-deleted instances, right? But if I query for all A instances? will it brings me just A instances with non-deleted B?

    Thanks!

    Reply

Leave a comment -