Search code examples
grailsgrails-orm

Grails loading data once


I have these 2 domains

class Country {
    String name
    static hasMany = [cities:City]
}

class City {

    String name;
    static belongsTo = [country: Country]
   }

The data contained in these 2 tables is relatively big, and they're used in all the screens, every time i choose a country i have to reload all its cities.

How can I load the data only once in memory so i can access it faster in all the screens.

I tried putting cities for eager fetching, and tried using cache plugin.

Thank you


Solution

  • You can configure both domain classes to be cached automatically and also cache the cities relation in Country:

    class Country {
        String name
        static hasMany = [cities:City]
    
        static mapping = {
            cache true
            cities cache:true
        }
    }
    
    class City {
        String name
        static belongsTo = [country: Country]
    
        static mapping = {
            cache true
        }
    }