[英]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. 我有一个遗留数据库,用于在grails 2.2.1应用程序中映射域对象。
The table I'm using contains the FK relationship back to itself for it's children. 我正在使用的表将FK关系返回给它的孩子。 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. Hibernate将t0.class放到选择列表中,然后失败。
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. 看起来像是这样草率/ hacky的解决方案。 If anyone has any ideas or thoughts I'd love to hear them. 如果有人有任何想法或想法,我希望能听到。
Thx 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.