Grails:具有级联的复合键在保存对象时会导致错误

时间:2013-09-05 15:02:31

标签: grails

我有两个域类,A和B.A有一个复合键。其中一部分指向B.我想设置级联:该字段的“全部”:

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

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

0 个答案:

没有答案