Question

I'm new to Groovy and Grails but I am pretty familiar with Hibernate. For a project I'm working on I need to implement soft deleting of certain domain classes. Hibernate Filters seemed to be an appropriate solution as it would allow me to add a condition to my domain class which would filter out the soft deleted records without us manually adding a condition to every query. Luckily, Grails offers a Hibernate Filter plugin which should make it easy to use Hibernate Filters in Grails.

Unfortunately, as you might've guessed, I can't get it to work. When debugging everything seems to be set up properly (filters being added to entities, filters are being enabled in sessions etc.) but nothing is being filtered from the results.

I've set it up like this:

BuildConfig.groovy

grails.project.dependency.resolution = {

    ...

    plugins {

        ...

        compile ":hibernate-filter:0.3.2"
    }
}

app-config.groovy

import org.grails.plugin.hibernate.filter.HibernateFilterDomainConfiguration

dataSource {

    ...

    configClass = HibernateFilterDomainConfiguration
}

MyDomainClass.groovy

class MyDomainClass extends MySuperClass {

    ...

    Boolean deleted = false

    static hibernateFilters = {
        deletedFilter(condition:"deleted=0", default:true)
    }

    ...

}

MyController.groovy

def list() {

    ...

    myDomainInstances = MyDomainClass.list()
    [myDomainInstanceList: myDomainInstances, myDomainInstanceTotal: myDomainInstances.totalCount, ...]
}

Using the above code always gives me unfiltered results even though everything seems to be configured properly. When debugging the application start-up I see the plugin being initialized, scanning my domain classes and picking up the hibernateFilters property from it. After this it properly creates the Hibernate filters. When receiving a request I see the Grails filter kicking in and the plugin enables all the default filters (which mine is) in the current Hibernate session. Everything looks as I would expect it (I see the filter being enabled in Hibernate's logging), except for the results (the SQL logging backs me up in this, no WHERE clauses are added for deleted).

I've followed the instructions on the documentation page and also tried the following:

I've also tried previous versions of the plugin and even tried explicitly enabling the filters using the plugins functions (Foo.withHibernateFilters etc.).

I'm still stuck though, so: could anyone help me get filtered results using the Grails Hibernate Filter plugin?

Used software version:

  • Grails 2.1.3
  • Grails Hibernate 2.1.3
  • Grails Hibernate Filter 0.3.2
  • Java 1.7.0_11
Was it helpful?

Solution

Grails 2.1.3 uses Hibernate version 3.6.10. Hibernate 3 does not support filters on subclasses (e.g. union subclasses, join subclasses). Adding the filter on MySuperClass in the above example will work. Hibernate 4.1.5.SP1 and newer versions will support filters on subclasses.

MySuperClass.groovy

class MySuperClass {

    ...

    Boolean deleted = false

    static hibernateFilters = {
        deletedFilter(condition:"deleted=0", default:true)
    }

    ...

}

MyDomainClass.groovy

class MyDomainClass extends MySuperClass {

    ...

}

Unfortunately, the above isn't properly documented in the Hibernate documentation regarding filters.

More information:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top