How to adjust constraints / DB mapping for Map within grails domain class

前端 未结 2 1423
生来不讨喜
生来不讨喜 2020-12-11 07:05

Following grails domain class:

class MyClass {
  Map myMap
}

Now for myMap, grails automatically creates a new table for the elements in th

相关标签:
2条回答
  • 2020-12-11 07:10

    what are you trying to accomplish? Is there always the same number of things in the map? If there is you should define those properties on your class.

    You can see the problem with your current approach -- there is no way to figure out what might be in the map until runtime, so how can grails possibly create a columns for it? Im surprised it even worked to begin with...

    0 讨论(0)
  • 2020-12-11 07:24

    An alternative approach I used successfully was to push the map out into a collection of a collaborator domain class.

    class DynaProperty {
        String name
        String value
    
        static belongsTo = MyClass
        static constraints = {
            value(maxSize:4000)  //Or whatever number is appropriate
        }
    }
    

    And then in MyClass:

    class MyClass {
        static hasMany = [dynaProperties:DynaProperty]
    }
    

    This is almost a map, and it gives you the ability to use dynamic finders to pull up an individual entry.

    0 讨论(0)
提交回复
热议问题