2012-07-30 49 views
1

我有以下域類(縮短版)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

回答