开发者

Grails MongoDB Plugin: Embedded Collections vs References

开发者 https://www.devze.com 2023-01-26 08:38 出处:网络
Originally posted on the Grails mailing list... I\'m not generally a database guy but with the release of the latest mongodb plugin for grails, I wanted to see what the big deal is with noSQL databas

Originally posted on the Grails mailing list...

I'm not generally a database guy but with the release of the latest mongodb plugin for grails, I wanted to see what the big deal is with noSQL databases. MongoDB seems pretty interesting. I was reading the information on Document oriented storage and came across the following scenario:

Customer / Order / Order Line Item

The doc says orders should be a collection. customers a collection. line-items should be an array of line-items embedded in theorder object.

With regards to GORM, how is this pattern ensured? I'd typically开发者_如何学C have the following:

class Customer {
    static hasMany = [orders: Order]
}

class Order {
    static hasMany = [orderItems: OrderItem]
    static belongsTo = [customer:Customer]
}

class OrderItem {
    static belongsTo = [order:Order]
}

How do I make sure that Orders is its own collection and not embedded within Customer? If that is the default, how to I make sure that OrderItems are embedded in Order and not it's own collection? What is the default here?

Thanks.


From reading the Grails MongoDB Plugin documentation, it seems like you need to specifically declare embedded objects, with references being the default.

With that in mind, if you want to make sure that Orders to be its own collection, and OrderItems to be embedded, try this:

class Customer {
  List<Order> orders
}

class Order {
  List<OrderItem> orderItems
  static embedded = [ 'orderItems' ]
}

class OrderItems {
  // properties go here.   
}

Here's the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消