Grails的 '假',唯一的错误

Grails的 '假',唯一的错误

问题描述:

我有以下域类(缩短版)Grails的 '假',唯一的错误

class TalkingThread { 

    static hasMany = [comments:Comment] 
    Set comments = [] 
    Long uniqueHash 
} 

class Comment { 
    static belongsTo = [talkingThread:TalkingThread] 
    static hasOne = [author:CommentAuthor] 
    Long uniqueHash 

    static constraints = { 
     uniqueHash(unique:true) 
    } 
} 

class CommentAuthor { 
    static hasMany = [comments:Comment] 
    Long hash 
    String name 
    String webpage 
} 

以下方法

public TalkingThread removeAllComments(TalkingThread thread){ 
    def commentsBuf = [] 
    commentsBuf += thread.comments 
    commentsBuf.each{ 
     it.author.removeFromComments(it) 
     thread.removeFromComments(it) 
     it.delete() 
    } 
    if(!thread.save()){ 
     thread.errors.allErrors.each{ 
      println it 
     } 
     throw new RuntimeException("removeAllComments") 
    } 
    return post 
} 

public addComments(TalkingThread thread, def commentDetails){ 
    commentDetails.each{ 
     def comment = contructComment(it,thread) 
     if(!comment.save()){ 
      comment.errors.allErrors.each{ println it} 
      throw new RuntimeException("addComments") 
     } 
     thread.addToComments(comment) 
    } 
    return thread 
} 

有时我需要删除TalkingThread中的所有评论并添加共享相同uniqueHashes的注释。所以我拨打removeAllComments(..)的方法,然后在上添加注释(..)的方法。这导致一个

Comment.uniqueHash.unique.error.uniqueHash这是由一个据称删除的评论和'新'评论被添加。

我应该冲洗吗?也许我的域名类有问题?

编辑问题的扩展。

也许这是一个不同的问题,但我认为会议已删除所有关联和对象。因此会话状态意识到所有TalkingThread评论已被删除。当然这并未反映在数据库中。考虑到这种“保存”与会话状态一致,我还假定新评论的“保存”是有效的。但是,这种“保存”与数据库状态不一致。因此,我理解Grails如何验证与会话和数据库状态相关的对象是有缺陷的!对于理解会话和数据库状态的验证过程的任何帮助也将被赞赏。

+0

flush至少在''it.delete()'做'it.delete(flush:true)'而不是 – Chris 2012-07-30 20:43:00

如果你想从TalkingThread中删除所有Comment,那么你可以使用Hibernate的级联行为。

添加

static mapping = { 
     comments cascade: 'all-delete-orphan' 
    } 

TalkingThread,然后你可以调用comments.clear()其次thread.save()这将删除已在该协会的意见。

有一个good article on Grails one-to-many-relationships hereofficial Grails docs on it are here.