Reputation: 11327
Can anyone tell me what would be a good way to implement soft deletes? I can have a deleted
property in my class, but my question is how to easily ignore instances which have deleted = true
in my searches, listings and so on.
So, rather than saying Domain.findByDeleted(true)
just get Domain.list()
to ignore deleted instances, rather than saying Domain.findByPropertyAndDeleted('property', true)
just say Domain.findByProperty('property')
.
Is there a nice way of doing this?
Upvotes: 3
Views: 1345
Reputation: 187409
The hibernate filter plugin can automatically add the predicate deleted = false
to every query that is performed for a particular domain class. However, my testing indicated that this plugin does not work with Grails 2.0.0.
Upvotes: 1
Reputation: 13495
We're used to override list()
, get()
and some more methods of domain classes. Now we can use syntax like A.delete(log: true)
On bootstrap, we do:
grailsApplication.domainClasses.each { GrailsDomainClass domainClassInfo ->
def oldGormDelete = domainClassInfo.metaClass.getMetaMethod('delete', [] as Class[])
assert oldGormDelete
domainClassInfo.metaClass.delete = { Map params ->
...
def result = oldGormDelete.invoke(delegate)
...
}
}
Upvotes: 0
Reputation: 19702
I would recommend using a named query for this. Something like
static namedQueries = {
notDeleted {
ne 'deleted', true
}
}
which you could use like Domain.notDeleted.list()
or Domain.notDeleted.findByProperty(value)
Upvotes: 8