Search code examples
grailsgrails-ormbelongs-tograils-domain-class

How does one mix 'Reference' and 'No Reference' belongTo relationships in one Domain Class?


In Grails belongsTo allows one domain class to establish a cascading relationship with another domain class. There are two styles of relationships when using belongsTo: Reference and No Reference. Reference creates a property on the owned object while No Reference merely establishes an invisible GORM relationship.

Example parent domain-class:

class Car {
    Engine engine
}

belongsTo without Reference property:

class Engine {  
    static belongsTo = Car  
}

belongsTo with Reference property:

class Engine {  
    static belongsTo = [car:Car]
}

Not to hard right, however the trouble for me starts when we start using multiple belongsTo references:

belongsTo with multiple back references:

class Engine {
     static belongsTo = [car:Car, user:User]
}

multiple belongsTo relationships without property references:

class Engine {
     static belongsTo = [Car, User]
}

Here's the problem, how do I mix the two above styles?

Say I want a property reference for the User but not for the Car, how would I write that belongsTo call?

Any information on how to mix No Reference relationship links with Reference property in a single domain class would help.

Links:


Solution

  • class Engine {
         User user
         static belongsTo = [Car, User]
    }
    

    That said, I always use the map (reference) syntax over the list (no reference) syntax because I like mine to be bi-directional.