How do you bulk delete records in Grails/GORM?

2019-03-17 12:26发布

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 delete the records, but that fails because there is no delete method on criteria...

def c = Agency.createCriteria()
c.delete
{
    eq("agency", "XXX")  
}

So I thought maybe I first query for the set and then delete that...

def c = Agency.createCriteria()
def deletions = c
{
    eq("agency", "XXX")  
}
deletions.delete

This also fails for the same reason, different object.

So what is the right way to do this? It seems excessive (perverse) that I would have to iterate through the entire result set calling delete() on each item.

I know I can form a query to execute directly either in HQL or SQL but that feels wrong too. Is the criteria builder only meant for retrieval?

Thanks

3条回答
Evening l夕情丶
2楼-- · 2019-03-17 12:32

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.

查看更多
唯我独甜
3楼-- · 2019-03-17 12:43

If you want to avoid HQL I'd suggest using GORM list(), delete() and Groovy's spread operator:

def agencyList = Agency.createCriteria().list {
    eq("agency", "XXX")  
}
agencyList*.delete()
查看更多
叛逆
4楼-- · 2019-03-17 12:56

From the User Guide about deleting objects:

Note that Grails does not supply a deleteAll method as deleting data is discouraged and can often be avoided through boolean flags/logic.

If you really need to batch delete data you can use the executeUpdate method to do batch DML statements:

Customer.executeUpdate("delete Customer c where c.name = :oldName", [oldName:"Fred"])
查看更多
登录 后发表回答