Grails:如何将外键设为主键

问题描述 投票:0回答:2

我有 2 个领域类; A和B.

class A {

    Long a_id

    static constraints = {
    }

    static mapping = {
        id name:'a_id'
    }
}

Class B {

    A a

    static constraints = {
    }

    static mapping = {
        id name:'a',  generator: 'assigned'
    }

}

在域 B 中,我想将 'a' 作为主键和外键(引用 A.a_id)

上面的代码不起作用。请帮我。

mysql hibernate grails grails-orm
2个回答
1
投票

您可以使主键始终与外键相同。并将外键指向主键。

Class B {

    A a

    static mapping = {
         id generator: 'foreign', params: [property: 'a']
         a insertable: false, updateable: false , column:'id'
    }

}

0
投票

“复合”映射不需要多个属性名称。提供与另一个域类对应的单个属性名称会生成正确的模式以使用外键作为主要模式。

外域必须实现Serializable才能使用复合映射

Class B implements Serializable { 
    A a

    static mapping = {
         id composite: ['a'] // Generates a column named 'a_id' and primary key index
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.