2014-02-19 38 views
0

具有Hibernate域類和集成測試的Grails/Groovy項目。 我有兩個類:grails集成測試中的HQL更新查詢

First{ 
    id 
    SomeClassAlsoSevaedInDB porp1 
    SomeClassAlsoSevaedInDB porp2 
    String somefield; 
} 

Second { 
    id 
    First first 
    String somefield2; 
} 

我要刪除第一種類型的唯一對象,因爲我在DB約20KK首先對象,我執行HSQL語句像

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

這種行爲我有錯誤:

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

如果我切換HQL查詢來更新第二個目的,它工作正常

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

但我需要HQL更好的性能比較

我試着這樣做:

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) 

我認爲,問題的根源情況下與刪除的第一個對象相關的對象仍包含在內存(緩存或別的東西, )和之前保存的東西做到這一點,但我不知道是什麼。

回答

0

如果您也使用HQL刪除Firsts,它們將不會被加載,所以您肯定不會得到與級聯相關的異常。

+0

我不能這樣做,因爲我需要級聯刪除,第一類也有其他類保存在數據庫 – user1680222