GORM:Grails 域 class 映射块中的 reference:true 是什么?
GORM: What is reference:true in Grails domain class mapping block?
public class Address {
static mapWith = "mongo"
Region region;
Zone zone;
static mapping = {
id generator: 'identity'
region reference:true
zone reference:true
}
}
我很想知道 reference:true
的作用。
根据我的经验,除了在实际 mongo 文档中没有 DBRef
之外,将其关闭会得到完全相同的结果。
这意味着这些属性将通过引用存储在您的地址记录中。当您查询数据库时,Region 的 id 和 Zone 的 id 将存在于记录中,而不是存储整个对象的映射及其映射可能包含的任何对象。返回地址对象看起来像这样:
{
"id": "2413",
"region": DBRef("region", "1234"),
"zone": DBRef("zone", "4321")
}
对于非嵌入式关联,默认情况下 MongoDB 的 GORM 将使用 MongoDB 数据库引用(也称为 DBRefs)映射文档之间的链接。如果您不想使用 DBRefs,那么您可以通过 reference:false 映射告诉 GORM 使用直接链接。
看起来 reference
控制文档的链接方式。
当true
时,相关文档被db-refs引用,如果false
,GORM在[=20中插入简单的id
,又名Manual references
=]
public class Address {
static mapWith = "mongo"
Region region;
Zone zone;
static mapping = {
id generator: 'identity'
region reference:true
zone reference:true
}
}
我很想知道 reference:true
的作用。
根据我的经验,除了在实际 mongo 文档中没有 DBRef
之外,将其关闭会得到完全相同的结果。
这意味着这些属性将通过引用存储在您的地址记录中。当您查询数据库时,Region 的 id 和 Zone 的 id 将存在于记录中,而不是存储整个对象的映射及其映射可能包含的任何对象。返回地址对象看起来像这样:
{
"id": "2413",
"region": DBRef("region", "1234"),
"zone": DBRef("zone", "4321")
}
对于非嵌入式关联,默认情况下 MongoDB 的 GORM 将使用 MongoDB 数据库引用(也称为 DBRefs)映射文档之间的链接。如果您不想使用 DBRefs,那么您可以通过 reference:false 映射告诉 GORM 使用直接链接。
看起来 reference
控制文档的链接方式。
当true
时,相关文档被db-refs引用,如果false
,GORM在[=20中插入简单的id
,又名Manual references
=]