简体   繁体   中英

HQL update query in grails integraion test

Grails/Groovy project with Hibernate domain classes and integration tests. I have two classes:

First{
    id
    SomeClassAlsoSevaedInDB porp1
    SomeClassAlsoSevaedInDB porp2
    String somefield;
}

Second {
    id
    First first
    String somefield2;
}

I want to delete only object of First type, due to I have about 20kk First objects in db, I execute hsql statement like

firstToDelete = [1,2,3]
Second.executeUpdate("Update Second set first = null where first.id in (1,2,3)")
First.findAllByIdInList(firstToDelete)*.delete()

With this behavior i have error:

deleted object would be re-saved by cascade (remove deleted object from associations): [First]

If i switch hql query to update Second objects it works fine

Second.findAllByFirstInList(First.findAllByIdInList(firstToDelete)).each {
    it.first = null
    it.save()
}
First.findAllByIdInList(firstToDelete)*.delete()    //  works fine

But i need HQL for better perfomance

I tried do this:

def listSecond = Second.findAllByFirstInList(First.findAllByIdInList(firstToDelete))
Second.executeUpdate("Update Second set first = null where first.id in (1,2,3)")
First.findAllByIdInList(firstToDelete)*.delete()
println listSecond.first // return Not Empty list(should be empty)

I think root case of issue is object related with deleted First object still contains in memory(cached or something else) and before save something do with this, but i don't know what.

如果您也删除带有HQL的First,则将不会加载它们,因此您当然不会获得与级联相关的异常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM