Oscar B
Oscar B

Reputation: 21

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? Searching the error I found this http://jira.grails.org/browse/GRAILS-11065 I'm using grails 2.3.7 by the way.

Upvotes: 2

Views: 707

Answers (1)

D&#243;nal
D&#243;nal

Reputation: 187499

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. So from GORM's perspective your model above is equivalent to

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.

Upvotes: 2

Related Questions