我碰到这个Nabble thread这可能类似于你以后跑了。这是一个有趣的解决方案,看起来很有趣,但可能会工作正常。
这是线程的代码示例(由Martin Dow提供 - 如果他运行此过程并将其作为答案发布,我将从我的答案中删除它,并给他upvote)。
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
实现需要定义多少逻辑。这两个例子都可以工作,也可能有其他解决方案。
我对为这组非常类的类声明一个超类非常谨慎,但是我发现像Java中的Object类这样的超类会起作用 - 我简单地声明了一个空的超类Something并让Idea和Person扩展它。如果需要,我可以让Person和Idea成为其他领域类的超类。鸭子键入来承担,因为我不必去实施一个抽象的东西类。感谢您帮助我考虑这一点。 – 2011-05-25 12:06:35