如何在Grails / Groovy中以一对多关系清除和替换集合 [英] How do I clear and replace a collection in a one-to-many relationship in Grails/Groovy
问题描述
这个问题分为两部分,第一部分是清除列表,第二部分是关于将一个所有者分配给一个对象。
我有一对一在我的模型中Grails中的两个域对象之间的多种关系。关系看起来像这样...
class Person {
static hasMany = [权限:角色,位置:位置]
}
类位置{
static belongsTo = Person
}
在我的应用程序中位置列表
Person
会完全刷新,并替换为用户操作中的新列表。更重要的是,我得到独立于关联的
Person
的位置
对象列表。我通过检索当前登录的用户来解析应用他们的人,我称之为 activePerson
,并且适合我的用途。
我想要做的是删除 activePerson
上的所有现有位置,并插入所有新的位置,将它们与 activePerson
随我去。我在 Person
上有一堆属性,我不想在这一点上坚持,所以我想避免保存整个父对象,因为位置子节点已经更改。
我想通过遍历 activePerson.locations
列表并逐个删除它们,并依靠GORM / Hibernate将查询一起批处理并在最后冲洗。这看起来像蛮力,尽管它可能有效。我期待有一个 clearLocations
方法或类似的东西,但我找不到一个。
如果我只是用新列表替换 activePerson.locations
,并调用 activePerson.save(flush:true)
,将GORM处理现有行的删除?
其次,我想将新的位置
对象放到 activePerson
。再次,我可以填充 activePerson.locations
并保存所有内容,但是如果可以的话,我想避免这种情况。我可以单独保存它们,但是如何在每个位置
对象上设置 belongsTo
?
回顾:
- 如何清除一个列表的多个末尾如何将独立对象与父对象关联并单独保存它们?
谢谢
结算集合
为我工作:
activePerson.locations.clear()
$ b $ 只需通过调用 collect()$ c来避免
ConcurrentModificationException
$ c $:
activePerson.locations.collect()。each {
item.removeFromLocations(it)
然后保存实体执行SQL语句:
activePerson.save flush:true
链接至文章以阅读更多内容:
http://spring.io/blog/2010/07/02/gorm-gotchas-part-2/
This question is in two parts, first part is about clearing a list and second part is about assigning an owner to an object.
I have a one-to-many relationship between two domain objects in my model in Grails. The relationship looks like this...
class Person {
static hasMany = [authorities: Role, locations: Location]
}
class Location {
static belongsTo = Person
}
In my app the locations
list on Person
gets completely refreshed and replaced with a new list on a user action. What's more I get the list of Location
objects independent of the associated Person
. I resolve which person to apply them to by retrieving the currently logged in user, which I call activePerson
and is fine for my purposes.
What I want to do is delete all the existing locations on activePerson
and insert all the new ones, associating them with the activePerson
as I go. I have a bunch of properties on Person
which I don't want to persist at this point, so I want to avoid saving the whole parent object just because locations children have changed.
I thought of iterating throught the activePerson.locations
list and deleting them one by one and relying on GORM/Hibernate to batch the queries together and flush at the end. This seems like brute force, although it might work. I was expecting there to be a clearLocations
method or something similar, but I can't find one.
If I do just replace the activePerson.locations
with a new list and call activePerson.save(flush:true)
, will GORM handle the delete of the existing rows?
Secondly I want to put the new Location
objects onto activePerson
. Again I could populate the activePerson.locations
and save the whole thing, but I would like to avoid that if I can. I can save them individually, but how do I set belongsTo
on each Location
object as I go?
So to recap:
- How do I clear a list at the many end of a one-to-many collection?
- How do I associate independent objects with a parent object and persist them individually?
Thanks
Clearing Collection
approach didn't worked for me:
activePerson.locations.clear()
Just try iterate over collection with avoiding ConcurrentModificationException
by calling collect()
:
activePerson.locations.collect().each {
item.removeFromLocations(it)
}
And after that save the entity to execute SQL statement:
activePerson.save flush:true
Link to article to read more: http://spring.io/blog/2010/07/02/gorm-gotchas-part-2/
这篇关于如何在Grails / Groovy中以一对多关系清除和替换集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!