Plugins You can find out about all the publicly available Grails plugins.

Hibernate Filter plugin

6 votes

3% of Grails users

compile "org.grails.plugins:hibernate-filter:0.3.2"

 Documentation  Source  Issues


Integrates Hibernate filtering into Grails


grails install-plugin hibernate-filter


This plugin allows Hibernate filters to be used in a GORM domain model.

Please check the plugin descriptor to determine the most recent release, because the version displayed on this page is unreliable.

This plugin currently does not work with WebFlow.


This page describes the versions of the plugin that are compatible with Grails 1.X and 2.X. Information about the Grails 3.X version is available here.


The plugin allows you to easily define hibernate filters on classes and associations within the domain class. Filters can be disabled/enabled anywhere within the code. Filters can also be designated as a default filter to be automatically applied. This allows smart defaults for security purposes.

This is very useful to enforce security rules outside of the GORM methods. Filters can also be applied to collections so that views don't have to enforce security without the use of DTOs.


Install the plugin and change one line in the grails-app/conf/DataSource.groovy file:

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


Hibernate filters are configured in domain classes (grails-app/domain):

class Member {
    boolean enabled
    boolean visible

static hasMany = [images: Image, pens: Pen]

static hibernateFilters = { enabledFilter(condition:'enabled=1', default:true) validFilter(condition:'enabled=1 and visible=1', aliasDomain:'ValidMember') enabledFilter(collection:'images', default: true) yearParam(condition: ':myParam = year', types: 'string') pensFilter(collection: 'pens', joinTable: true) } }

  • The first line creates a default filter called 'enabledFilter' which the condition 'enabled=1'
  • The second line creates a non-default filter called 'validFilter' with an alias domain 'ValidMember'
  • The third line creates a default filter that uses the same condition as the first one and applies it to the association 'images'
Filters with the same name use the same condition even if not in the same domain class

Filters are not applied to Domain.get() because .get() is not considered a query. If filters should be applied when loading an object by ID, use Domain.findById() instead


  • condition - The filter condition
  • default - true to enable this filter by default (or a closure returning true or false)
  • collection - the collection (association) to apply this filter to
  • aliasDomain - A domain name to use when you want to apply this filter in a single instruction.
Example: ValidMember.findAllByName('user1') - Will find only members matching the validFilter with the name 'user1'

Join tables

Since version 0.4.0, you are required to indicate any filtered associations that are mapped via a join table with joinTable: true In the example above, joinTable: true is specified for the pens collection, but not the images collection, because only the former is mapped via a join table. Additional examples are available in the test cases.

Injected methods

Several methods are injected into grails domain classes

withHibernateFilter - execute the enclosed code with the named filter enabled

Foo.withHibernateFilter('filterName') {
    ..code to execute

withHibernateFilters - execute the enclosed code with all default filters enabled

Foo.withHibernateFilters {
    ..code to execute

withoutHibernateFilter - execute the enclosed code with the named filter disabled

Foo.withoutHibernateFilter('filterName') {
    ..code to execute

withoutHibernateFilters - execute the enclosed code with all default filters disabled

Foo.withoutHibernateFilters {
    ..code to execute

enableHibernateFilter - enable a Hibernate filter (returns the Hibernate org.hibernate.Filter instance)


def filter = Foo.enableHibernateFilter('filterWithArgs') filter.setParameter('argName', 42)

disableHibernateFilter - disable a hibernate filter


Usage for filter with parameter

Book.enableHibernateFilter('yearParam').setParameter('myParam', '2008')

Enables the 'yearParam' filter and sets the parameter to '2008'.


Given the above domain configuration


  • Member (name:'user1', enabled:true)
    • Image (name:'image1', enabled:false)
    • Image (name:'image2', enabled:true)
    • Image (name:'image3', enabled:true)
  • Member (name:'user2', enabled:false)
  • Member (name:'user3', enabled:true, visible:true)
class MemberController {

def sessionFactory

def index = { Member.withoutHibernateFilter('enabledFilter') { println Member.list() // returns all members }

// renders a view with members and images where enabled=true [members: Member.list()] } }


    <g:each in="${members}" var="member">
        member: ${} (enabled:${member.enabled})<br/>
        <g:each in="${member.images}" var="image">
            image: ${} (enabled:${image.enabled})<br/>


member: user1 (enabled:true)
image: image2 (enabled:true)
image: image3 (enabled:true)

member: user3 (enabled:true)

feedback is always welcome - scott (at) bulldoginfo (dot) com

Version History

  • 0.4.0 (May 18, 2016) Upgrade to Hibernate 4 and Grails 2.5. Several bugfixes and improvements
  • 0.3.2 (July 18, 2012) - Bug fix for onChange events
  • 0.3.1 (Mar 1, 2012) - Bug fix for collections and subclassing (GPHIBERNATEFILTER-2)
  • 0.3 (Feb 29, 2012) - Better support for named parameters (GPHIBERNATEFILTER-1) and support for Grails 2.0+
  • 0.2 (Jun 6, 2010) - Added ability to have named parameters in filter (Thanks to Jean-Guy)
  • 0.1.7 (Dec 7, 2009) - Fixed bug when using domains in packages
  • 0.1.6 (nov 17, 2009) - Removed hibernate dependency that was causing problems. Moved injected methods to be static domain methods.
  • 0.1.2 (Nov 3, 2009) - Fixed NPE when domain classes are in a package
  • 0.1.1 (Nov 2, 2009) - Added support for aliasDomain names
  • 0.1 (Nov 1, 2009) - Initial release