简体   繁体   English

如何在grails域类中为Map调整约束/数据库映射

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

Following grails domain class: 以下是grails域类:

class MyClass {
  Map myMap
}

Now for myMap, grails automatically creates a new table for the elements in the map. 现在,对于myMap,grails自动为地图中的元素创建一个新表。 However if I add elements which are too long (eg 1024 characters), I get a DB error. 但是,如果添加的元素太长(例如1024个字符),则会出现数据库错误。

Can I somehow tell grails to make the respective column in myMap's table big enough to allow for larger Strings, or do I have to do this manually in the DB? 我可以通过某种方式告诉grails使myMap表中的相应列足够大以允许使用更大的String,还是必须在数据库中手动执行此操作?

I already tried 我已经试过了

static constraints = {
  myMap(maxSize:1024)
}

which doesn't work (as expected because maxSize should refer to the Map's values and not to the Map itself). 这是行不通的(正如预期的那样,因为maxSize应该引用Map的值而不是Map本身)。

If not via constraints, maybe there's a way to do it via 如果不是通过约束,也许有办法通过

static mapping { ... }

?

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: 然后在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. 几乎是一张地图,它使您能够使用动态查找器提取单个条目。

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? 您可以看到当前方法的问题-在运行时之前无法弄清楚映射中可能包含的内容,那么grails如何为其创建列呢? Im surprised it even worked to begin with... 我很惊讶它甚至从一开始就起作用了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM