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

Grails:带有级联的复合键在保存对象时会导致错误,grails,Grails,我有两个域类,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 st

我有两个域类,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()
    }
}
class B {
    String name = "Test"
}
B.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()
    }
}
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

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

cascade
行为将对关联起作用,但在您的情况下,您在
A
中定义了
B
,这将在表
A
中创建
B\u id
(比如)列。您可以在
B
中使用
static belongsTo=[A]
来获取级联性质,而不是
A
中的
cascade all
。我删除了
cascade all
并添加了
static belongsTo=[A]
,但得到了相同的错误。同样,当我不使用复合键时,它也可以工作。