sunnyrjuneja
sunnyrjuneja

Reputation: 6123

Hibernate Filter is not applied in Grails Service/Jobs

My domain uses a hibernate filter like so:

class UserMapping {
    // Senseless App Logic here
    static hibernateFilters = {
        typeFilter(condition: "type = 'IDENTIFIED'", default: true)
        flagFilter(condition: "flags = 'ACTIVE'", default: true)
    }
}

They seem to apply fine when I use them in the controller however when I do the same findBy.. in my service or job, they are not applied. Any suggestions?

If it makes any difference, I invoke the findBy in a mixin.

This question seems related but its not exactly what I'm looking for: enabling grails hibernate filters

Upvotes: 3

Views: 303

Answers (2)

RMorrisey
RMorrisey

Reputation: 7739

In my case, I found that I forgot the "default: true" flag. :o

We also have code in BootStrap.groovy which enables the hibernate filters:

def init = { servletContext ->
    //Hibernate filters
    MyDomainObject.enableHibernateFilter('myFilter')
}

(I realize the question was already answered but I hope this helps someone else that's looking.)

Upvotes: 1

Burt Beckwith
Burt Beckwith

Reputation: 75671

The only time filters are automatically enabled is during web requests - this is due to the plugin's HibernateFilterFilters applying all enabled filters before each web request. But there's no global automatic filter logic - this would be impractical.

Instead use one of the approaches described in the plugin documentation, e.g.

UserMapping.withHibernateFilters {
   ..code to execute
}

Upvotes: 2

Related Questions