0

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

2 Answers 2

3

In order to make this bidirectional you need to add static belongsTo = [house: House] and remove your House house property from the Roof domain.

You can read more about the belongsTo property in the documentation.

1
  • Tried but same result. If fact, according to GORM, hasOne is always bidirectional. I also tried as a property in class House { Door door } and class Door { static belongsTo = [ house: House ]} which neither worked. Oct 8, 2014 at 7:39
0

The problem was that inverse side wasn't initialized.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.