当试图保存一个由 Mongo 支持的 Grails 域对象时,我所有的嵌入对象也都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)
这种动作似乎会导致嵌入实体,Bird
并被Tree
保存为值,但似乎也被保存以更新其关联的域模型。
有没有办法可以禁止更新这些引用并只将嵌入的字段保存在TreeState
对象中?