I am trying to define a Grails domain model with abstract classes. I need to define two abstract classes that have a one-to-one bidirectional relationship with each other and can't bring them to work.
Explanation based on the Face-Nose example of the documentation:
I implemented the example and wrote a test that works as expected: if I set an end of the relationship, grails sets the other end.
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
If I declare these two classes as abstract and repeat the same test with two concrete subclasses (ConcreteFace and ConcreteNose without any attribute), the second assertion is false: testNose.face is null.
Am I doing somthing wrong? If not, how can I factorize relationships in abstract domain classes?