Question

I have these 4 classes:

class Animal {
    static hasMany = [legs: Leg]
}

class Cat extends Animal {
    static hasMany = [legs: CatLeg] // Should I remove this line?
}

class Leg {
    static belongsTo = [animal: Animal]
}

class CatLeg extends Leg {
    static belongsTo = [animal: Cat] // Should I remove this line?
}

This would be okay? Is it necessary or recommended to declare these variables again in the subclasses? Which would be the difference between adding or omitting them?

I'm currently using Grails 2.2.2.

Thanks,

Jordi

Was it helpful?

Solution

This depends on your Hibernate settings. By default in Grails, Hibernate maps hierarchy of objects into 1 table (hierarchy per table). That means, you don't need to declare the reference again in the child classes. The GORM layer will take care about the correct class (Leg vs. CatLeg).

Generally, I would recommend to avoid the object hierarchy at all and replace it with composition, specially, if you have a hierarchy of referenced objects. I assume, that you can come to troubles, if the requirements change in future, regarding the data migration to a new model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top