2013-06-05 56 views
1

我正在構建一個Grails應用程序,當從MongoDB GORM插件切換到Hibernate插件時,通過集成測試得到了一些奇怪的結果。運行以下斯波克集成測試時MongoDB和Hibernate之間的級聯差異

class Client{ 
    //fields... 
    static hasMany = [workspaces: Workspace] 
} 

class Workspace{ 
    //fields... 
    static belongsTo = [client: Client] 
} 

然後:

def "Deleting a client removes its related workspaces"() { 

    given: "An existing client and workspace" 
    Client client = new Client().save(failOnError: true) 
    Workspace workspace = new Workspace().save(failOnError: true) 
    client.addToWorkspaces(workspace) 

    when: "Deleting the client" 
    def foundClient = Client.get(client.id) 
    foundClient.delete(flush: true) 
    assert !Client.exists(client.id) 

    then: "Related workspace is also deleted" 
    !Workspace.exists(workspace.id) 
} 

此測試將通過與Hibernate但與

我有一個一對多的關係的客戶端和工作區級MongoDB正在運行。 MongoDB中不會刪除工作空間和測試的最後一行將失敗,因爲這樣的:

Workspace.count() == 0 
      |  | 
      1  false 

有沒有辦法MongoDB的爲Hibernate的使用GORM執行相同的級聯操作?

感謝您的幫助

+0

您到達什麼解決方案? –

回答

0

我相信你可以使用GORM的beforeDelete攔截器,讓你要尋找的效果。例如:

def beforeDelete() { 
    Workspace.withNewSession { 
     def ws = Workspace.findByClient(id) 
     if (ws) { 
      ws.delete(flush:true) 
     } 
    } 
} 
相關問題