我遇到了这个Nabble 线程,它可能与您所追求的相似。这是一个有趣的解决方案,看起来有点老套,但可能会很好用。
这是来自该线程的代码示例(由Martin Dow提供- 如果他遇到此问题并将其作为答案发布,我将从我的答案中删除它并给他投票)。
class SomeDomainClass {
Long associationId
String associationClass
def getAssociation() {
// Handle proxied class names
def i = associationClass.indexOf('_$$_javassist')
if (i > -1) associationClass = associationClass[0..i-1]
getClass().classLoader.loadClass(associationClass).get(associationId)
}
def setAssociation(association) {
associationId = association.id
associationClass = association.class.name
}
}
这里有几个选项确实涉及可能给你一些方向的类型。
枚举
class Person {
static hasMany = [fondnessess: Something]
}
enum Something {
ONE_SOMETHING,
ANOTHER_SOMETHING;
}
遗产
(我不是继承的最大粉丝,但有时这是一个不错的解决方案。)
class Person {
static hasMany = [somethings: Something]
}
class Something {
// could be abstract, but look out for
// http://jira.grails.org/browse/GRAILS-6780
}
class OneSomething extends Something { ... }
class AnotherSomething extends Something { ... }
这取决于每个Something
实现需要定义多少逻辑。这两个示例都可以工作,并且可能还有其他解决方案。