1

I am building a Grails application and I am getting some weird results with integration testing when switching from the MongoDB GORM plugin to the Hibernate plugin.

I have a Client and Workspace class in a one to many relationship:

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

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

Then when running the following Spock integration test:

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)
}

This test will pass with Hibernate but not with MongoDB running. MongoDB will not delete the workspace and the last line of the test will fail as such:

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

Is there a way for MongoDB to perform the same cascading operations as Hibernate using GORM?

Thanks for your help

4

1 回答 1

0

我相信你可以使用beforeDeleteGORM 中的拦截器来获得你想要的效果。就像是:

def beforeDelete() {
    Workspace.withNewSession { 
        def ws = Workspace.findByClient(id)
        if (ws) {
            ws.delete(flush:true)
        }
    }
}
于 2013-07-06T03:00:57.340 回答