Grails 3& Mongo

时间:2017-11-15 16:13:06

标签: mongodb grails gorm-mongodb

试图找出是否有一种方法可以将这两个域对象与我连接到Oracle数据库时的方式相关联。

gradle.properties

grailsVersion=3.2.9
gradleWrapperVersion=2.9
gormVersion=6.1.3.RELEASE

的build.gradle

compile "org.grails.plugins:mongodb:6.1.3"
compile "org.mongodb:mongodb-driver:3.4.2"

域对象:

class Store {
  Long id
  // other properties
  Long sellerId
}

class Seller {
  Long id
  // other properties
}

我想做这样的事情:

class Store {
  Long id
  // other properties
  Long sellerId
  Seller seller
  Seller getSeller {
    Seller.findById(this.sellerId)
  }
}

在上面的情况中,只有sellerId持久保存到Mongo,因为它没有标记为embedded。如果我在grails代码中引用它,这非常有用 - 为store.seller中的所有属性提供有效值。但是,如果我从控制器返回store,则store.seller无法完全通过。商店的响应JSON看起来像这样(注意卖家只有id属性):

{
  id: 1,
  // other properties
  seller: {
    id: 22
  }
}

我也试过这样的事情,但是之后的事情永远不会被击中:

class Store {
  Long id
  // other properties
  Long sellerId
  Seller seller

  def afterLoad() {
    seller = Seller.findById(this.sellerId)
  }
}

还有更好的方法吗?

0 个答案:

没有答案