GORM不处理在复合主键中具有进一步关联的关联的映射 [英] GORM doesn't handle mapping of associations which have a further association in their composite primary key

查看:548
本文介绍了GORM不处理在复合主键中具有进一步关联的关联的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在GORM / Grails应用程序中映射现有表。大多数表都有复合主键(不是我的选择)。



我发现当我将一个关联映射到一个类父类)在父类的组合键包含一个具有组合键的类的关联(祖父母)的情况下,GORM不会检查祖父母类和子类在非组合键中的映射。

Child

 可序列化{
父母
字符串名称

belongsTo = [父:父]

静态映射= {
id(合成:[ 'parent','name'])
}
}

Parent

 类父类实现Serializable {
GrandParent grandParent
字符串名称
收集< Child> children

belongsTo = [grandParent:GrandParent]
hasMany = [children:Child]

static mapping = {
id(composite:['grandParent ','name'])
}
}

GrandParent

  class GrandParent实现Serializable {
字符串名称
整数luckyNumber
Collection<亲本GT​​;父母

hasMany = [父母:父母]

静态映射= {
id(合成:['name','luckyNumber'])
}
}

尝试收集外键时,DDL生成失败。

  org.hibernate.MappingException:
外键(FK_1:CHILD [parent_grandparent_id,parent_name]))
必须有与引用的主键
(PARENT [parent_grandparent_name,parent_grandparent_lucky_number,parent_name])

创建的外键与父类的主键(它可以正确解译)不匹配。 解决方案

我已经问过关于Grails Slack #gorm的问题,这似乎在Gorm映射中出现了一个空白。



我已经在Grails数据映射程序中引发了一个问题等。我们会看看这是怎么回事。


I'm trying to map existing tables in a GORM/Grails app. Most of the tables have composite primary keys (not my choice).

I'm finding that when I map an association to a class (parent of a child) where the composite key of the parent class contains an association to a class with a composite key also (grandparent), GORM doesn't bother to check the mapping of the grandparent class and subs in a non-composite key.

Child

class Child implements Serializable {
    Parent parent
    String name

    belongsTo= [parent: Parent]

    static mapping= {
        id(composite: ['parent', 'name'])
    }
}

Parent

class Parent implements Serializable {
    GrandParent grandParent
    String name
    Collection<Child> children

    belongsTo= [grandParent: GrandParent]
    hasMany= [children: Child]

    static mapping= {
        id(composite: ['grandParent', 'name'])
    }
}

GrandParent

class GrandParent implements Serializable {
    String name
    Integer luckyNumber
    Collection<Parent> parents

    hasMany= [parents: Parent]

    static mapping= {
        id(composite: ['name', 'luckyNumber'])
    }
}

The DDL generation fails when trying to glean the foreign keys.

org.hibernate.MappingException:
Foreign key (FK_1:CHILD [parent_grandparent_id,parent_name]))
    must have same number of columns as the referenced primary key
(PARENT [parent_grandparent_name,parent_grandparent_lucky_number,parent_name])

The foreign key it's created does not match the primary key of the parent class (which it was able to decipher correctly).

解决方案

I've asked around on Grails Slack #gorm, and this appears to hit a gap in Gorm mapping.

I've raised an issue in the Grails data mapping project. We'll see where this goes.

这篇关于GORM不处理在复合主键中具有进一步关联的关联的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆