Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement soft deletes

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?

like image 621
zoran119 Avatar asked Feb 15 '26 20:02

zoran119


2 Answers

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)

like image 196
doelleri Avatar answered Feb 18 '26 19:02

doelleri


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.

like image 41
Dónal Avatar answered Feb 18 '26 19:02

Dónal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!