【发布时间】:2011-10-25 14:14:17
【问题描述】:
我是 Grails&GORM 的新手,所以这可能是一个简单的问题。我们目前正在考虑使用 GORM 的 mongo 支持,我在映射到现有集合数据时遇到了一些问题。我基本上想映射到一个分层对象结构,其中我的对象“商家”引用了另一个父商家。 BSON 结构相当简单,即:
{
name: "name",
parent_id: ObjectId("[Object Id ref]")
}
在我的模型中,我尝试将这种关系映射如下:
class Merchant {
ObjectId id
String name
Merchant parent
static belongsTo = [parent: Merchant]
static mappedBy = [parent: "parentId"]
static mapping = {
collection "merchants"
}
static constraints = {
}
}
这导致了以下 BSON:
{
"_id" : ObjectId("4ea6be91ce5f56cd49f43ab8"),
"name" : "where will you g",
"version" : NumberLong(1),
"parent" : {
"$ref" : "merchants",
"$id" : ObjectId("4ea6be91ce5f56cd49f43ab8")
}
}
这有两个问题,即: - 父商家字段的名称是“parent”而不是“parent_id”,这是必填项。 - 除了 $ref 中的 id 之外,父字段的值还有其他元信息:“merchants”。
无论如何我可以保留我们现有的 BSON 结构并仍然拥有丰富的对象映射。
干杯,克里斯。
【问题讨论】:
-
似乎您可以通过为
parent_id“列”命名来解决部分问题:grails.org/doc/latest/guide/GORM.html#tableAndColumnNames 另外,也许您不想使用 mappedBy 或指定集合。也许那实际上应该是table 'merchants'而不是收藏。不是 100% 确定所有这些。
标签: grails mongodb grails-orm