如何在 grails 域类中调整 Map 的约束/数据库映射
以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我成功使用的另一种方法是将地图推送到协作者域类的集合中。
然后在 MyClass 中:
这几乎是一张地图,它使您能够使用动态查找器来提取单个条目。
An alternative approach I used successfully was to push the map out into a collection of a collaborator domain class.
And then in MyClass:
This is almost a map, and it gives you the ability to use dynamic finders to pull up an individual entry.
你想实现什么目标?地图上的东西数量总是相同吗?如果有,您应该在您的类上定义这些属性。
您可以看到当前方法的问题 - 在运行时之前无法弄清楚映射中可能有什么,那么 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...