简体   繁体   中英

Grails/Gorm - relating a domain object to itself 1:M

I have a legacy database which I am using to map domain objects in a grails 2.2.1 application.

The table I'm using contains the FK relationship back to itself for it's children. Luckily, I know I only have to go one level deep in the hierarchy.

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

This is possible resultset:

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

My domain objects looks like this:

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'
    }
}

The query doesn't work. Hibernate puts a t0.class into the select list, which it fails on.

Any advice?

Regards, Robin

This is messy, but I ended up solving the issue by creating another domain class that contains a reference to the original class. I'd like to find a better way, but this will work for now.

The database table I'm testing with:

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

My classes look like this:

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'
    }
}

When I do the following test it works great:

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

It just seems like such a sloppy / hacky solution. If anyone has any ideas or thoughts I'd love to hear them.

Thx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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