Grails 2抽象域继承问题 [英] Grails 2 abstract domain inheritance issue

查看:101
本文介绍了Grails 2抽象域继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails中使用抽象(或非抽象)继承时,以下方法对我不适用。



很快,我的继承如下所示:

 抽象BaseClass {...} 
SomeClass extends BaseClass {...}
SomeOtherClass extends BaseClass {...}

然后在另一个域对象中:

  ThirdClass {
...
BaseClass baseProperty
...
}

但是现在,当我尝试将该属性设置为 SomeClass SomeOtherClass 实例,Grails包含:


错误util.JDBCExceptionReporter - 无法添加或更新子行:外键约束失败
...

这是不可能的吗?






我也试过让基类不是抽象的,而且还试过casti将 SomeClass SomeOtherClass 实例更改为 BaseClass 。它们会产生相同的错误。




更新



我刚刚查过。它适用于我添加的第一个子类。但是,只要我尝试添加其他子类,它就会失败。



换句话说:

<$ p $ b $ def prop1 = new ThirdClass(baseProperty:instanceOfSomeClass).save()

工作正常。但是,当我尝试做:

pre $ def prop2 = new ThridClass(baseProperty:instanceOfSomeOtherClass).save()

失败。






更新2



进一步调查显示表创建过程中出现问题。它正确地将两个外键添加到 ThirdClass 表中,但是键错误地引用了:

  CONSTRAINT`...`FOREIGN KEY(`some_id`)参考`base_class`(`id`),
CONSTRAINT`...`FOREIGN KEY(`some_id`)REFERENCES`some_class`(`不知道为什么选择基类和其中一个子类?为什么选择基类和其中一个子类?我试过清洗等。

首先,创建您的BaseClass outside 域结构。它必须是一个外部类,放在脚本文件夹,源文件夹中。

  package com.example.model 

/ **
* @author Inocencio
* /
class BaseClass {

Date createdAt = new Date()

}

现在,创建一个常规的域类并扩展它。

  package com.example.domain 

import com.example.model.BaseClass
$ b $ class SomeClass extends BaseClass {

字符串名称

静态约束= {
名称(可空值:false)
}

} $ b $正如你所看到的,一旦你坚持SomeClass一个createdAt字段被填充和保存,以及。

检查测试课出:

  @TestFor(SomeClass)
类SomeClassTests {

void testSomething(){
def some = new SomeClass()
some.name =Hello There
some.save()

//找到它
def someFind = SomeClass.list()[0]

assert someFind

assertTrue someFind.createdAt!= null

// println Date:$ someFind.createdAt
// printlnName:$ someFind.name
}
}

我希望这可以帮上忙。


The following is not working for me when using abstract (or non-abstract for that matter) inheritance in Grails.

Very quickly, my inheritance is as follows:

abstract BaseClass               { ... }
SomeClass      extends BaseClass { ... }
SomeOtherClass extends BaseClass { ... }

And then in another domain object:

ThirdClass {
    ...
    BaseClass baseProperty
    ...
}

But now, when I try to set that property to either a SomeClass or SomeOtherClass instance, Grails compains:

ERROR util.JDBCExceptionReporter - Cannot add or update a child row: a foreign key constraint fails ...

Is this not possible?


I have also tried having the base class not be abstract, and also tried casting the SomeClass or SomeOtherClass instances to BaseClass. They generate the same error.


UPDATE

I just checked. It works for the first sub-class that I add. But as soon as I try to add the other sub-class it fails.

In other words:

def prop1 = new ThirdClass(baseProperty: instanceOfSomeClass).save()

works fine. But when I then try and do:

def prop2 = new ThridClass(baseProperty: instanceOfSomeOtherClass).save()

it fails.


UPDATE 2

Further investigation shows that something goes wrong during the table creation process. It correctly adds two foreign keys to the ThirdClass table, but the keys incorrectly references:

CONSTRAINT `...` FOREIGN KEY (`some_id`) REFERENCES `base_class` (`id`),
CONSTRAINT `...` FOREIGN KEY (`some_id`) REFERENCES `some_class` (`id`)

Don't know why it's choosing the base class and one of the sub-classes? I have tried cleaning etc.

解决方案

First of all, create your BaseClass outside domain structure. It must be an external class, put it on script folder, source folder.

package com.example.model

/**
 * @author Inocencio
 */
class BaseClass {

    Date createdAt = new Date()

}

Now, create a regular domain class and extend it.

package com.example.domain

import com.example.model.BaseClass

class SomeClass extends BaseClass {

    String name

    static constraints = {
        name(nullable: false)
    }

}

As you can see, once you persist SomeClass a createdAt field is filled and saved as well. Check the test class out:

@TestFor(SomeClass)
class SomeClassTests {

    void testSomething() {
        def some = new SomeClass()
        some.name = "Hello There"
        some.save()

        //find it
        def someFind = SomeClass.list()[0]

        assert someFind

        assertTrue someFind.createdAt != null

//        println "Date: $someFind.createdAt"
//        println "Name: $someFind.name"
    }
}

I hope it can be helpful.

这篇关于Grails 2抽象域继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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