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

发布于 2024-09-09 05:32:32 字数 458 浏览 1 评论 0原文

以下 grails 域类:

class MyClass {
  Map myMap
}

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

我可以以某种方式告诉 grails 让 myMap 表中的相应列足够大以允许更大的字符串,还是我必须在数据库中手动执行此操作?

我已经尝试过

static constraints = {
  myMap(maxSize:1024)
}

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

如果不通过约束,也许有办法通过

static mapping { ... }

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 { ... }

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐偏执 2024-09-16 05:32:32

我成功使用的另一种方法是将地图推送到协作者域类的集合中。

class DynaProperty {
    String name
    String value

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

然后在 MyClass 中:

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

几乎是一张地图,它使您能够使用动态查找器来提取单个条目。

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.

不必在意 2024-09-16 05:32:32

你想实现什么目标?地图上的东西数量总是相同吗?如果有,您应该在您的类上定义这些属性。

您可以看到当前方法的问题 - 在运行时之前无法弄清楚映射中可能有什么,那么 grails 如何为它创建列呢?我很惊讶它甚至从一开始就起作用了......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文