Grails:没有hasMany的域对象中的集合 [英] Grails: Collection in domain object without hasMany
问题描述
我有两个域对象A和B.
A可以与许多B关联,但我不想从A到B进行任何保存级联。
我正在考虑定义hasMany关系形式A到B,但随后设置级联行为。任何想法?
这是我的域对象的一个例子:
class A {
static hasMany = [bees:B]
}
class B {
}
为什么不在B上使用'belongsTo',并且不要在A上声明任何东西?这样你就可以将外键取到'A',但'A'上的操作不会影响'B'。你失去了一点方便,但仍然可以通过'A'轻松查找所有'B'。我其实更喜欢这个,因为我不需要担心延迟加载陷阱和休眠时间,并加载所有'B',当我只是试图添加一个(假设你不需要这个功能)。
class A {}
class B {
static belongsTo = [your_a:A]
}
//得到你的B给定的A
B.findAllByA(your_A_instance,...分页等等)
I have two domain objects A and B.
A can be associated with many Bs but I do not want any save cascade from A to B.
I'm thinking of defining the hasMany relationship form A to B, but then setting a cascade behavior. Any ideas?
This is an example of my domain objects:
class A{
static hasMany = [bees:B] }
class B{ }
Why not use 'belongsTo' on B, and don't declare anything on A? This way you'll get the foreign key to 'A', but operations on 'A' won't affect 'B'. You lose a bit of convenience, but can still easily look up all 'B' by 'A'. I actually prefer this because I don't need to worry about lazy loading gotchas and hibernate going and loading all the 'B's when I'm just trying to add one (assuming you don't need that functionality).
class A{}
class B{
static belongsTo = [your_a:A]
}
//get your B's for a given A
B.findAllByA(your_A_instance, ...paging, etc...)
这篇关于Grails:没有hasMany的域对象中的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!