我正在尝试使用抽象类定义 Grails 域模型。我需要定义两个彼此具有一对一双向关系并且不能使它们工作的抽象类。
基于文档的 Face-Nose 示例的解释:
我实现了该示例并编写了一个按预期工作的测试:如果我设置关系的一端,grails 设置另一端。
class Face {
static hasOne = [nose:Nose]
static constraints = {
nose nullable: true, unique:true
}
}
class Nose {
Face face
static belongsTo = [Face]
static constraints = {
face nullable:true
}
}
when:'set a nose on the face'
def testFace = new Face().save()
def testNose = new Nose().save()
testFace.nose = testNose
testFace.save()
then: 'bidirectional relationship'
testFace.nose == testNose
testNose.face == testFace
如果我将这两个类声明为抽象类并使用两个具体子类(没有任何属性的 ConcreteFace 和 ConcreteNose)重复相同的测试,则第二个断言为假:testNose.face 为空。
我做错了吗?如果不是,我如何分解抽象域类中的关系?