grails - Grails/Gorm-将域对象与自身1:M相关联

标签 grails gorm

我有一个遗留数据库,用于在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放到选择列表中,然后失败。

有什么建议吗?

问候,
罗宾

最佳答案

这很麻烦,但是我最终通过创建另一个包含对原始类的引用的域类来解决了该问题。我想找到一种更好的方法,但是现在可以使用。

我正在测试的数据库表:

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 |
+---+--------------+------------+

我的类(class)如下:
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的解决方案。如果有人有任何想法或想法,我希望能听到。

谢谢

关于grails - Grails/Gorm-将域对象与自身1:M相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15909076/

相关文章:

grails - Grails 中有 pre-GORM init 钩子(Hook)吗?

grails - Grails Oracle数据库无法添加新

grails - 不使用Neo4j GORM插件的Grails应用程序的Neo4j数据库

grails - Grails 3.3.0/Gorm:如何通过联接进行查询

regex - 考试评分字段的正确正则表达式

pdf - 发出请求后,停止 Controller 再次执行

grails - 在Grails中进行测试时,如何模拟忽略验证的域对象?

grails - Grails根据枚举列表属性中的枚举值选择域对象

grails - 从YAML通过关联加载GORM

model-view-controller - Grails生成一个zip文件供用户下载