如何实现软删除

时间:2012-01-19 00:36:15

标签: grails gorm grails-domain-class

有谁能告诉我实施软删除的好方法是什么?我的班级可以拥有deleted属性,但我的问题是如何轻松忽略我的搜索,列表等中deleted = true的实例。

所以,而不是说Domain.findByDeleted(true)只是让Domain.list()忽略已删除的实例,而不是说Domain.findByPropertyAndDeleted('property', true)只是说Domain.findByProperty('property')

这样做有什么好办法吗?

3 个答案:

答案 0 :(得分:7)

我建议使用named query。像

这样的东西
static namedQueries = {
    notDeleted {
        ne 'deleted', true
    }
}

您可以使用Domain.notDeleted.list()Domain.notDeleted.findByProperty(value)

答案 1 :(得分:1)

hibernate filter plugin可以自动将谓词deleted = false添加到为特定域类执行的每个查询中。但是,我的测试表明此插件不适用于Grails 2.0.0。

答案 2 :(得分:0)

我们习惯于覆盖list()get()以及其他一些域类方法。现在我们可以使用A.delete(log: true)

之类的语法

在bootstrap上,我们这样做:

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)
        ...
    }
}
相关问题