Convert Grails from Hibernate XML to GORM

I am migrating the grails project using Hibernate XML only to the GORM defined in the domain classes. One previous XML file defines a map:

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="myproj" default-lazy="true"> <class name="Season" table="seasons"> <cache usage="read-only"/> <comment>Season</comment> <id name="id" type="long"> <generator class="assigned"> </generator> </id> <property name="seasonKey" column="season_key"/> <many-to-one name="league" class="Affiliation" column="league_id"/> <many-to-one name="publisher" class="Publisher"/> // MAP STARTS HERE <map name="seasonWeeks"> <cache usage="read-write"/> <key column="season_id"></key> <map-key column="week" type="int"/> <one-to-many class="SeasonWeek"/> </map> </class> </hibernate-mapping> 

As you can see, it creates an Integer card, SeasonWeek. Earlier this code worked.

When I try to recreate a map in GORM, it does not work. Grails Documentation 1.3.7 (the version I'm on):

The hasMany static property determines the type of elements inside the Map. The keys for the card must be strings.

In my case, I do not want the map to be a string. Questions:

  • Is there a way to do what I want here? Maybe using static mapping var?
  • If not, do you need to restore the old Hibernate XML file? Can I do this only for one domain class in my project and not have XML files for the rest?

Thanks.

+4
source share
1 answer

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 } } 
+4
source

All Articles