RemoveFrom然后删除()

时间:2013-08-19 15:24:05

标签: hibernate grails gorm has-many

从hasMany关联中删除许多对象然后删除对象本身我有一个奇怪的问题。

我有域类网站和用户(我没有'belongsTo'关系):

class User {
}
class Website {
    static hasMany = [websiteUsers : User]
}

和这个方法:

void deleteUserFromWebsite(User user,Website website){
    log.debug "-----> deleteUserFromWebsite()"
    println website.websiteUsers
    website.removeFromWebsiteUsers(user)
    println website.websiteUsers
    user.delete(flush:true)
    log.debug "deleteUserFromWebsite ----->"
}

当我调用此方法一次时,一切正常,但是当我想在一个请求中同时进行批量操作(删除2个或更多用户)时,我有来自hibernate的错误:

deleted object would be re-saved by cascade (remove deleted object from associations)

这是因为hibernate不会将用户从网站中的关联中删除。

使用不同用户的两个方法调用记录:

2013-08-19 16:49:56,769 [http-bio-8080-exec-9] DEBUG website.WebsiteService  - -----> deleteUserFromWebsite)
[com.**.security.User : 17, com.**.security.User : 3, com.**.security.User : 12, com.**.security.User : 21, com.**.security.User : 16]
[com.**.security.User : 17, com.**.security.User : 3, com.**.security.User : 21, com.**.security.User : 16]
2013-08-19 16:49:56,777 [http-bio-8080-exec-9] DEBUG website.WebsiteService  - deleteUserFromWebsite ----->
[com.**.security.User : 17, com.**.security.User : 3, com.**.security.User : 21, com.**.security.User : 16]
2013-08-19 16:49:56,811 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver  - ObjectDeletedException occurred when processing request: [POST] /**/**/** - parameters:
users: 12,16
websiteId: mol24o
deleted object would be re-saved by cascade (remove deleted object from associations): [com.**.security.User#16]. Stacktrace follows:

在第一次调用时,hibernate从关联中删除用户(userId:12),但是第二次,id为16的用户没有从有多少删除然后当我要删除对象时,当然是休眠抛出异常。

有没有办法让hibernate刷新他的缓存并更新websiteUsers?

1 个答案:

答案 0 :(得分:0)

你是如何称呼“群众行动”的?该错误表示您正在使用(或具有引用)您尝试删除的用户。

例如,如果您在删除时循环访问websiteUsers集合,则会导致此错误。

请查看GORM Gotchas以获得对此的非常好的解释。