How do you bulk delete records in Grails/GORM?

前端 未结 3 562
一生所求
一生所求 2021-02-05 05:15

I have a table which has records that need to be periodically cleared according to a set of criteria.

I was expecting that I could use the criteria builder to just delet

3条回答
  •  感动是毒
    2021-02-05 05:45

    With Grails 2.0 you can use a detached query like this:

    Agency.where { }.deleteAll()
    

    Note that you don't get the listeners and whatnot executed, but it does execute through to the database, AND it is compatible with the mocked domain stuff, as in:

    void testWhatever() {
        mockDomain(Agency, [])
        saveABunchOfAgencies() // saves 10 of 'em
        assert Agency.count() == 10
    
        Agency.where { }.deleteAll()
    
        assert Agency.count() == 0   // Joy!
    }
    

    That being said the GORM unit test mocks have a bunch of gotchas but are in general pretty neat.

提交回复
热议问题