【问题标题】:Grails/Gorm - relating a domain object to itself 1:MGrails/Gorm - 将域对象与自身关联 1:M
【发布时间】:2013-04-09 17:53:31
【问题描述】:

我有一个遗留数据库,我用它来映射 grails 2.2.1 应用程序中的域对象。

我正在使用的表包含 FK 关系返回到它自己的孩子。幸运的是,我知道我只需要在层次结构中深入一层。

T_FOO
----------------------
I                 LONG
CHILD_FOO         LONG

这是可能的结果集:

I  CHILD_FOO
-  ---------
1  NULL
2  NULL
3  1
4  1
5  2

我的域对象如下所示:

class Foo {
    long instanceId
    static hasMany = [childFoos: Foo]

    static mapping {
        table 'T_FOO'
        id generator: 'assigned', name: 'instanceId', column: 'I', type: 'long'
        version false
        autoTimestamp false
        instanceId column: 'I'

        // I tried joining the table to itself and it didn't work
        childFoos joinTable [name: 'T_FOO', key: 'CHILD_FOO', column: 'I'
    }
}

查询无效。 Hibernate 将 t0.class 放入选择列表中,但它失败了。

有什么建议吗?

问候, 罗宾

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    这很麻烦,但我最终通过创建另一个包含对原始类的引用的域类来解决问题。我想找到一个更好的方法,但这暂时可行。

    我正在测试的数据库表:

    mysql> desc t_foo;
    +-------------+-------------+------+-----+---------+-------+
    | Field       | Type        | Null | Key | Default | Extra |
    +-------------+-------------+------+-----+---------+-------+
    | I           | bigint(20)  | NO   | PRI | NULL    |       |
    | Description | varchar(45) | YES  |     | NULL    |       |
    | I_CHILDFOO  | bigint(20)  | YES  |     | NULL    |       |
    +-------------+-------------+------+-----+---------+-------+
    
    mysql> select * from t_foo;
    +---+--------------+------------+
    | I | Description  | I_CHILDFOO |
    +---+--------------+------------+
    | 1 | Parent 1     |       NULL |
    | 2 | Parent 2     |       NULL |
    | 3 | Child 1 of 1 |          1 |
    | 4 | Child 1 of 1 |          1 |
    | 5 | Child 1 of 2 |          2 |
    +---+--------------+------------+
    

    我的课程如下所示:

    package db.legacy
    
    class Foo {
    
        long instanceId
        String info
    
        static hasMany = [ kids: ChildFoo ]
    
        static constraints = {
        }
    
        static mapping = {
            table 'T_FOO'
            id generator: 'assigned', name: 'instanceId', column: 'I', type: 'long'
            version false
            autoTimestamp false
            instanceId column: 'I'
            info column: 'Description'
            kids column: 'I_CHILDFOO'
        }
    }
    
    class ChildFoo {
    
        long instanceId
        Foo parentFoo
    
        static mapping = {
            table 'T_FOO'
            id generator: 'assigned', name: 'instanceId', column: 'I', type: 'long'
            version false
            autoTimestamp false
            instanceId column: 'I'
            parentFoo column: 'I_CHILDFOO'
        }
    }
    

    当我进行以下测试时,效果很好:

    def foo = db.legacy.Foo.get(1)
    foo.kids.each { bar -> 
        assert bar.parentFoo.instanceId == foo.instanceId
    }
    

    这似乎是一个草率 / hacky 的解决方案。如果有人有任何想法或想法,我很想听听。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 2017-07-13
      • 2013-09-17
      • 2016-06-01
      • 1970-01-01
      • 2019-12-27
      • 2013-03-23
      相关资源
      最近更新 更多