mysql - Grails:如何将外键设为主键

标签 mysql hibernate grails grails-orm

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

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

最佳答案

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

Class B {

    A a

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

}

关于mysql - Grails:如何将外键设为主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46005746/

相关文章:

php - 我想知道是否有一种方法可以显示android studio中当前日志的特定数据和使用mysql数据库的用户

grails - Grails 2.2.5-域对象未保存,但没有错误

spring - 即使端点设置为 permitAll,也会检查不记名 token

交换属性值后,Grails 唯一测试失败

MYSQL根据过滤器获取图片的所有常用标签

PHP 表单需要点击两次提交按钮才能消失

php - 在mysql表中搜索5个关键字

java - 表上的 UniqueConstrait 在 Hibernate 中不起作用

java - 是什么导致我的 GWT 应用程序出现 HTTP 503 错误?

java - 对 child 有限制的 hibernate 标准