Search code examples
hibernategrailsgrails-orm

How to add hibernate filter in Grails 4


I want to add global hibernate filter for a specific domain to keep my queries as clean as possible.

In previous projects I used the grails-hibernate-filter plugin which is not working/available for the latest GORM version.

How could I manually add hibernate fiter to a specific domain class?

I wasn't able to find a documentation for that.

Additions based on comments:

The current application already uses discriminator based multi-tenancy to support multiple tenants. Now I need further distinctions for each tenant.

Taking this example domain:

class Vehicle implements MultiTenant<Vehicle> { 
    String manufacturer
    String color

    static mapping = {
        tenantId name:'manufacturer'  
    }
}

Currently a user just gets all vehicles for its tenant. Now I want to provide a way that the user will just get vehicles for its tenant and a specific color. I don't want to add a color condition for each query. Instead I want to add a hibernate filter to Vehicle. Using the hibernate plugin I would be able to do something like:

static hibernateFilters = {
    colorFilter(condition: 'color = :color', types: 'string')
}

Solution

  • Instead of try adding hibernate filter manually, I found a fork of the grails-hibernate-filter plugin which is working with Grails 4.

    Seems to work as intended. At least for my current requirements.