*仅*在建立关系后保留 Grails 域
Persist Grails Domains *ONLY* after Relationship built
我想知道是否可以创建一个 grails 域对象,但只让它在命令上持久存在,而不是在我们对其进行操作时。
更准确地说,这是我现在要做的:
Foo foo = new Foo(name:"asdf")
Bar bar = new Bar(name:"gzxj")
bar.save() // persist here
foo.save() // persist here
foo.addToBars(bar) // now I have to build the relationship
我想要的:
Foo foo = new Foo(name:"asdf")
Bar bar = new Bar(name:"gzxj")
foo.addToBars(bar) // just build the relationship
bar.save() // would be great to ignore this step
foo.save() // can I now atomically build both objects and create the relationships?
我的印象是,如果要关联的关系很多,后者会快得多。我真的只想要 NoSQL 吗?
这完全有可能,这取决于您建立人际关系的方式。真的和你实现的是什么数据库没有关系。
Parent class
class Foo {
String name
static hasMany = [bars: Bar]
}
Child class
class Bar {
String name
Foo foo //optional back reference
static belongsTo = Foo
}
执行
Foo foo = new Foo(name: 'a')
Bar bar = new Bar(name: 'b')
foo.addToBars(bar)
foo.save()
//or even more terse
Foo foo = new Foo(name: 'c')
foo.addToBars(new Bar(name: 'd'))
foo.save()
关键是 belongsTo
,默认为 all
的 cascade
。这也可以明确设置:
class Foo {
String name
static hasMany = [bars: Bar]
static mapping = {
bars cascade: 'all'
}
}
我想知道是否可以创建一个 grails 域对象,但只让它在命令上持久存在,而不是在我们对其进行操作时。
更准确地说,这是我现在要做的:
Foo foo = new Foo(name:"asdf")
Bar bar = new Bar(name:"gzxj")
bar.save() // persist here
foo.save() // persist here
foo.addToBars(bar) // now I have to build the relationship
我想要的:
Foo foo = new Foo(name:"asdf")
Bar bar = new Bar(name:"gzxj")
foo.addToBars(bar) // just build the relationship
bar.save() // would be great to ignore this step
foo.save() // can I now atomically build both objects and create the relationships?
我的印象是,如果要关联的关系很多,后者会快得多。我真的只想要 NoSQL 吗?
这完全有可能,这取决于您建立人际关系的方式。真的和你实现的是什么数据库没有关系。
Parent class
class Foo {
String name
static hasMany = [bars: Bar]
}
Child class
class Bar {
String name
Foo foo //optional back reference
static belongsTo = Foo
}
执行
Foo foo = new Foo(name: 'a')
Bar bar = new Bar(name: 'b')
foo.addToBars(bar)
foo.save()
//or even more terse
Foo foo = new Foo(name: 'c')
foo.addToBars(new Bar(name: 'd'))
foo.save()
关键是 belongsTo
,默认为 all
的 cascade
。这也可以明确设置:
class Foo {
String name
static hasMany = [bars: Bar]
static mapping = {
bars cascade: 'all'
}
}