【问题标题】:GORM: What is reference:true in Grails domain class mapping block?GORM:Grails 域类映射块中的 reference:true 是什么?
【发布时间】:2023-03-07 00:59:01
【问题描述】:
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

【问题讨论】:

    标签: grails grails-orm grails-domain-class


    【解决方案1】:

    看起来reference 控制文档的链接方式。

    true时,相关文档被db-refs引用,如果false,GORM在mongo中插入简单的id,又名Manual references

    【讨论】:

    • 感谢注入者!使用 GORM 使用 db-refs 引用的文档有什么好处吗?手册说“除非您有令人信服的理由使用 DBRefs,否则请改用手动引用。”
    • 来自同一个文档:By including these names, DBRefs allow documents located in multiple collections to be more easily linked with documents from a single collection。因此,如果您需要链接单个集合中的文档,手动参考就可以了
    • 但是可以在域类定义中找到该信息。所以 GORM 通过查看字段的类就知道要查找哪个集合了,对吧?
    • 总的来说,这两个选项都有效,但我认为 db-ref 可能会更好地优化
    • @AlexanderSuraphel 也看看这个thread,了解为什么在 mongodb 中使用引用不是一个好主意
    【解决方案2】:

    这意味着这些属性将通过引用存储在您的地址记录中。当您查询数据库时,Region 的 id 和 Zone 的 id 将存在于记录中,而不是存储整个对象的映射及其映射可能包含的任何对象。返回地址对象看起来像这样:

    {
      "id": "2413",
      "region": DBRef("region", "1234"),
      "zone": DBRef("zone", "4321")
    }
    

    默认情况下,对于非嵌入式关联,GORM for MongoDB 将使用 MongoDB 数据库引用(也称为 DBRefs)映射文档之间的链接。如果您不想使用 DBRefs,那么您可以使用 reference:false 映射告诉 GORM 使用直接链接。

    Gorm Mapping
    Searchable Reference

    【讨论】:

    • 你给的链接是关于Searchable的,我没有提到。
    • 我更新了答案以显示指向更多相关内容的链接。 Searchable 引用使用与 Gorm 映射相同的 reference:true 映射。我认为这个概念可以来自那个可搜索的链接,不管答案是一样的。
    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2021-08-01
    • 2015-12-19
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多