« Return to Thread: Strange behavior on bidirectional one-to-one relations

Strange behavior on bidirectional one-to-one relations

by stefano gualdi :: Rate this Message:

Reply to Author | View in Thread

Hi all,

maybe I'm doing something stupid, but I've the following problem.

Given the following sample domain classes:

class Folder {
    FolderSpec specification
    String name
    static constraints = {
        name(nullable: false)
    }
}

class FolderSpec {
    static belongsTo = [folder: Folder]
    String details
    static constraints = {
        details(nullable: false)
    }
}

If i create a new Folder object with this code:

def spec = new FolderSpec(details:'dummy')
def folder = new Folder(name:'MainFolder')
folder.specification = spec
folder.save(flush:true)

the reference 'folder' on FolderSpec remains null. While, using this:

def spec = new FolderSpec(details:'dummy')
def folder = new Folder(name:'MainFolder', specification: spec)
folder.save(flush:true)

everything works as expected.

The only way to obtain the correct behavior in the first example is adding a setter method on Folder:

void setSpecification( FolderSpec spec ) {
        this.specification = spec
        if (spec) {
            this.specification.folder = this
        }
}

Is this an expected behavior?

I've verified this on grails 1.0.1 and on SVN 6743.

Thanks in advance

stefano

 « Return to Thread: Strange behavior on bidirectional one-to-one relations