如何在Grails域类中调整Map的约束/ DB映射 [英] How to adjust constraints / DB mapping for Map within grails domain class

查看:205
本文介绍了如何在Grails域类中调整Map的约束/ DB映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的grails域类:

  class MyClass {
Map myMap
}

现在,对于myMap,grails会自动为地图中的元素创建一个新表。但是如果我添加太长的元素(例如1024个字符),我会得到一个DB错误。



我可以告诉grails,



我已经尝试过了

  static constraints = {
myMap(maxSize:1024)
}

这不起作用(如预期的那样,因为maxSize应该引用Map的值,而不是Map本身)。



通过约束,也许有办法通过

 静态映射{...} 



解决方案

我成功使用的是将地图推送到协作网域类的集合中。

  class DynaProperty {
String name
字符串值

static belongsTo = MyClass
static constraints = {
value(maxSize:4000)//或任何数字适当
}
}

然后在MyClass中:

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

这是几乎一个地图,它允许您使用动态查找器来拉取单个条目。


Following grails domain class:

class MyClass {
  Map myMap
}

Now for myMap, grails automatically creates a new table for the elements in the map. However if I add elements which are too long (e.g. 1024 characters), I get a DB error.

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?

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).

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:

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.

这篇关于如何在Grails域类中调整Map的约束/ DB映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆