Grails MongoDB嵌入式域对象保存

时间:2015-11-18 23:48:57

标签: mongodb grails gorm-mongodb

当尝试保存由Mongo支持的Grails Domain对象时,我的所有嵌入对象都会调用save()。在某些情况下,这会对性能产生巨大影响。

一些示例域对象如下。

class Bird {
    Object Id
    String name
}

class Nest {
    static embedded = ['birds']

    String name
    Set<Bird> birds
}

class Tree {
    static embedded = ['nests']

    Object Id
    String name
}

class TreeState {
    static embedded = ['tree']

    Object Id
    Set<Nest> nests
    Tree tree
    Date dateCreated
}

现在假设我想在给定时间保存树的状态。

def tree = Tree.findByName("Sleepwood")

def nestA = new Nest()
nestA.birds = new LinkedHashSet()
nestA.name = "Sleepwood-A"

def nestB = new Nest()
nestB.birds = new LinkedHashSet()
nestB.name = "Sleepwood-B"

def blueJay = Bird.findByName('Blue Jay')
def cardinal = Bird.findByName('Cardinal')
def oriole = Bird.findByName('Oriole')
def robin = Bird.findByName('Robin')

def treeState =  new TreeState()
treeState.nests =  new LinkedHashSet()
treeState.tree = tree

nestA.add(blueJay)
nestA.add(cardinal)
nestA.add(oriole)

nestB.add(oriole)
nestB.add(blueJay)
nestB.add(robin)

treeState.nests.add(nestA)
treeState.nests.add(nestB)
treeState.safe(failOnError: true)

此类操作似乎会导致嵌入的实体BirdTree保存为值,但似乎也会保存以更新其关联的域模型。

有没有办法可以禁止更新这些引用,只保存TreeState对象中的嵌入字段?

0 个答案:

没有答案