Search code examples
grailsgrails-orm

Grails hasOne relations are really bidirectional?


I want to do reflexion on Domain associations to automatically generate a JSON/XML format class descriptor file for a RESTful WS (scaffolding). In the case of HasOne (supposedly bidirectional), I am trying to use the referencedPropertyName in the Association object to show the reverse key (that's the only information that the WS has to show).

Nevertheless I find that hasOne Associations are not correctly initialized.

What follows is the test I made using Grails 2.3.7.

My domains are:

class House {

    Long number
    Integer inhabitants

    static hasOne = [ roof: Roof ]
    static hasMany = [ doors: Door ]

    static constraints = {
        roof nullable: true, unique: true
    }
}

class Roof {

    String color
    House house

    static constraints = {
    }
}

Then:

import org.grails.datastore.mapping.model.types.Association
import org.junit.Test

import spock.lang.*
import test.House
class AssociationsSpec {
    @Test
    void "test something"() {
        House.gormPersistentEntity.associations.each { Association association ->
            String key = association.name
            println association.properties
            assert association.bidirectional
        }
    }
}

This test fails:

Assertion failed: 
assert association.bidirectional
       |           |
       |           false
       test.House->roof
    at test.AssociationsSpec$_test_something_closure1.doCall(AssociationsSpec.groovy:26)
    at test.AssociationsSpec.test something(AssociationsSpec.groovy:23)

Additional info (println association.properties):

[list:false, type:interface java.util.Set, owner:org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity@149a797, class:class org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity$5, cascadeOperations:[ALL], inverseSide:test.Door->house, capitilizedName:Doors, bidirectional:true, referencedPropertyName:house, associatedEntity:org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity@99a9c3, mapping:null, circular:false, owningSide:true, name:doors, fetchStrategy:EAGER]

[capitilizedName:Roof, bidirectional:false, referencedPropertyName:null, circular:false, owningSide:true, name:roof, list:false, type:class test.Roof, owner:org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity@149a797, class:class org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity$4, cascadeOperations:[ALL], inverseSide:null, associatedEntity:org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity@16289cc, mapping:null, foreignKeyInChild:true, fetchStrategy:EAGER]

Thank you


Solution

  • The problem was that inverse side wasn't initialized.

    Fixed in: https://github.com/grails/grails-data-mapping/commit/3c6b7f296be359742062c26113d2ce5c617167c7