简体   繁体   English

共享相同主键名称的遗留表的 Grails 域映射

[英]Grails domain mapping of legacy tables that share same primary key name

The legacy database has two tables that use the same column name as the primary key.旧数据库有两个表,它们使用与主键相同的列名。 For example:例如:

user:用户:
user_id int user_id 整数
name string名字串

user_profile:用户资料:
user_id int user_id 整数
visit_count int访问计数整数

I would like to create a bidirectional one-to-one relationship.我想创建一个双向的一对一关系。 ie IE

class User {
    String name
    UserProfile userProfile
    static mapping = {
        id column: 'user_id'
    }
}

class UserProfile {
    Integer visitCount
    User user
    static mapping = {
        id column: 'user_id'
    }
}

I would like to be able to reference 'user.userProfile.visitCount' or 'userProfile.user.name'.我希望能够引用“user.userProfile.visitCount”或“userProfile.user.name”。

I have tried many combinations of direct references and relationship discriptors, 'hasOne', 'belongsTo', etc. I would think this is simple but cannot find the right syntax.我尝试了许多直接引用和关系描述符的组合,“hasOne”、“belongsTo”等。我认为这很简单,但找不到正确的语法。 I get duplicate column issues or missing column name issues.我遇到重复的列问题或缺少列名问题。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance.提前致谢。

The User in UserProfile would default to user_id which is the same as the id column for UserProfile. UserProfile 中的 User 将默认为 user_id,这与 UserProfile 的 id 列相同。 So just add a column name for the user reference like below.因此,只需为用户引用添加一个列名,如下所示。

class UserProfile {
    Integer visitCount
    User user

    static mapping = {
        id column: 'user_id'
        user column: 'useruser_id'
    }
}

class User {
    String name

    static mapping = {
        id column: 'user_id'
    }

    static hasOne = [ userProfile: UserProfile ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM