grailsgroovygrails-ormgrails-domain-class

Grails One-To-Many unidirectional without join table is it possible?


Grails One-To-Many unidirectional without creating join table is not working. I tried as per Documentation mentioned here.

When I tried creating an Domain class as per the documentation its creating the Join Table. Is this a bug or my understanding is wrong?. Here is the code which I used

class Person {
    String bookName
    static hasMany = [addresses: Address]
    static mapping = {
        addresses column: 'person_address_id'
    }
 }

 class Address {
    String address
    static constraints = {
    }
}

And the resulting table EER Model is

enter image description here

NOTE: Using datasource.dbCreate as "create-drop" in application.yml file.


Solution

  • The unidirectional mapping w/o join table is only possible, if you invert the relation between your domain classes:

    class Person {}
    
    class Address {
      static belongsTo = [ person:Person ]
    }
    

    I usually go that way, as it has the better performance and consistency comparing to others.

    The only thing which is missing out-of-box is give me all addresses for person-case, which can be modelled relatively easy:

    class Person {
    
      List<Address> getAddresses( params = [:] ) {
        Address.findAllByPerson this, params
      }
    
    }