【问题标题】:Grails GORM hasMany association inconsistent with MongoDB, When try to fetch child Records,it fetches the record sometimes and doesn't fetch sometimesGrails GORM有很多和MongoDB不一致的关联,当尝试获取子记录时,有时会获取记录,有时不获取
【发布时间】:2018-06-25 08:46:12
【问题描述】:

下面是域类Author,它与Book 具有一对多关联。在尝试获取 Author 时,有时会得到 Book 域对象的关联集合,有时会返回 null。关于为什么不一致的任何想法?

class Author { static mapWith = "mongo" String name static hasMany = [books: Book] }

_author.gson

model { Author author } json g.render(author]) { books g.render(author.books) }

环境详情:

grailsVersion=3.3.5 gormVersion=6.1.8.RELEASE

我们正在使用 GORM 多租户并使用 MongoDB 数据库。

【问题讨论】:

  • 在黑暗中拍摄,添加 List books = new ArrayList(); 会发生什么在您的域类作者中?
  • @JasonHeithoff 不确定,如果List<Book> books = new ArrayList() 将保存books 数据。 static hasMany = [books: Book]List<Book> books = new ArrayList() 仅相同。让我也试试你的解决方案。完成测试后,我会在这里更新。
  • @JasonHeithoff 我已经尝试过您的解决方案,但没有成功。

标签: mongodb hibernate grails grails-orm


【解决方案1】:

我已切换到子文档模型,它现在可以工作了。这是域模型代码。

class Author {
    static mapWith = "mongo"
    String name
    List<Books> book
    static embedded = ['book'] 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多