1

我有两个域类,A 和 B。A 有一个复合键。其中一部分指向 B。我想为该字段设置 cascade:'all':

A.groovy

class A implements Serializable {        
    Integer a = 1 // This points to another class in my real code. I tried to make it as simple as possible. It has the same effect.
    B b

    static mapping = {
        // It works fine if I comment out the following line (i.e. no composite key)
        id composite:['a', 'b']        
        b cascade: 'all'
    }

    boolean equals(other) {
        if (!(other instanceof A)) {
            return false
        }
        other.a == a && other.b == b
    }

    int hashCode() {
        def builder = new HashCodeBuilder()
        builder.append a
        builder.append b
        builder.toHashCode()
    }
}

B.groovy

class B {
    String name = "Test"
}

当我尝试在 Bootstrap.groovy 中运行以下代码时,出现异常:

B b = new B()
// It works fine if the next line is uncommented
//b.save(failOnError:true)

A a = new A()
a.b = b
a.save(flush:true, failOnError:true)

错误:

| Error 2013-09-05 16:53:35,308 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: null
Message: null
    Line | Method
->>   13 | doCall                           in BootStrap$_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    308 | evaluateEnvironmentSpecificBlock in grails.util.Environment
|    301 | executeForEnvironment . . . . .  in     ''
|    277 | executeForCurrentEnvironment     in     ''
|    334 | innerRun . . . . . . . . . . . . in java.util.concurrent.FutureTask$Sync
|    166 | run                              in java.util.concurrent.FutureTask
|   1145 | runWorker . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    615 | run                              in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . . . . . . . . . . . .  in java.lang.Thread

我知道不鼓励使用复合键,但我真的很想知道这里有什么问题。

4

0 回答 0