简体   繁体   English

在 grails 域类上使用泛型时出错

[英]Error using generics on grails domain class

I'm trying to create a generic abstract hierarchy implementation, the code is the following:我正在尝试创建一个通用的抽象层次结构实现,代码如下:

abstract class AbstractHierarchy<T> {
    T parent

    static hasMany = [children: T]

    static constraints = {
        parent(nullable: true)
    }

    static mapping = {
        tablePerHierarchy false
    }
}

Im getting this error:我收到此错误:

AbstractHierarchy.groovy: -1: The class java.lang.Object refers to the class java.lang.Object and uses 1 parameters, but the referred class takes no parameters.

So the question is, Am I doing something wrong?所以问题是,我做错了什么吗? is this supported by grails?这是 grails 支持的吗? Searching the error I found this http://jira.grails.org/browse/GRAILS-11065 I'm using grails 2.3.7 by the way.搜索错误,我发现了这个http://jira.grails.org/browse/GRAILS-11065顺便说一下,我正在使用 grails 2.3.7。

On account of type erasure, I would expect that everywhere you are using T in your domain model, you might as well be using Object .由于类型擦除,我希望您在域模型中使用T任何地方,您都可以使用Object So from GORM's perspective your model above is equivalent to所以从 GORM 的角度来看,你上面的模型相当于

abstract class AbstractHierarchy {
    Object parent

    static hasMany = [children: Object]

    static constraints = {
        parent(nullable: true)
    }

    static mapping = {
        tablePerHierarchy false
    }
}

which I assume is not what you intended.我认为这不是你想要的。 So the moral of the story is: don't use generics to express relationships between your domain classes.所以这个故事的寓意是:不要使用泛型来表达域类之间的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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