I found that trying to get some things to work in grails from older applications using GORM is simply not the easiest thing. You can find a solution that matches 99% of your problems, but you will spend a lot of time trying to find an easy button for the last 1% of problems (for example, yours). I think you could do this using the transition field as the key , for example ( note that there are two solutions, I recommend the second ) ...
class Season { static mapping = { seasonWeeks mapKey: 'weekOfYearAsString' } static hasMany = [seasonWeeks:SeasonWeek] Map seasonWeeks = [:] } class SeasonWeek{ ... String name Integer weekOfYearAsInt String weekOfYearAsString String getWeekOfYearAsString(){ return String.valueOf(weekOfYearAsInt); } void setWeekOfYearAsString(def value){ this.weekOfYearAsInt = Integer.parseInt(value); } }
ALSO, you can completely exclude GORM ( what would I do ), and just handle creating a map for your season ...
class Season{ ... public Map getSeasonWeeksMap(){ Map map = [:] def seasons = SeasonWeek.findBySeason(this.id) season.each(){season -> map.put(season.id, season) } return map } }
source share