【问题标题】:Grails: How to make a foreign key as primary keyGrails:如何将外键作为主键
【发布时间】:2018-02-10 20:15:12
【问题描述】:

我有 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)

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

【问题讨论】:

  • 我可能是错的,但您可以尝试在您的域 B 中添加 belongsTo = [a: A]

标签: mysql hibernate grails grails-orm


【解决方案1】:

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

Class B {

    A a

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

}

【讨论】:

  • 这简直太棒了。虽然,可以省略 id generator: ... 行,它可以按原样工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
相关资源
最近更新 更多