【问题标题】:GRAILS - GORM : DuplicateKeyException during saving a new objectGRAILS - GORM:保存新对象期间出现 DuplicateKeyException
【发布时间】:2015-10-20 09:29:48
【问题描述】:

我使用 GORM 从 excel 文件中支持数据库中的出现。

new ExcelBuilder(excelFile.inputStream).eachLine([labels: true, sheet: 0]) {
                if (cell(0)) {
                    def nameA = cell(0)
                    def nameB = cell(1)
                    def a = Chapitre.findByNom(nameA)

                def code = cell(2)
                def designation = cell(3)

                if (code == null || nameA == null || nameB == null) {
                    flash.messages << "error"
                } else if (!Chapitre.findByNom(nameA)) {
                    flash.messages << "error"
                } else if ( Rubrique.where{nom == nameB && chapitre == a}.list().size() == 0) {
                    flash.messages << "error"
                } else if(Object.where{rubrique == Rubrique.findByNom(nameB) && c == code && d == designation}.count() > 0){
                    flash.messages << "error"
                } else {

                        def b = Rubrique.findByNom(nameB)
                        def isNew = false;

                        Object.withNewSession {session2->
                            def object = Object.findOrCreateByCode(code)

                            if(object.designation == null)
                                isNew = true;

                            object.rubrique = b
                            object.d= (designation == null)?"":designation
//                              try {
                                    rowCount += object.save()? 1 : 0
//                              } catch(ValidationException) {
//                                    if(isNew)
//                                        rowCount++;
//                                    log.info("ErreuRRRRRRRRrrrRRrrRrRRrrrrRrrrRrrrRrrrr")
//                              }
                            }
                    }
                }
                currentLine++
}
flash.messages << "${rowCount} ligne create or update"

一更新就打消后顾之忧,文件行继续,数据库记录有效。

但是在插入新对象时,我得到一个异常:

org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session:[fen.NuisanceType#2202]; 
nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

有问题的对象的注册是有效的,但是文件的路径引发了错误。

当我取消注释 try 和 catch 时,我绕过了错误,因此每个文件的所有副本都在数据库中创建。

我因此找到了解决我的担忧的方法,但我觉得它不是很干净,我来找你试图了解我的问题。

【问题讨论】:

    标签: spring hibernate grails grails-orm


    【解决方案1】:

    没有进一步的信息很难给出任何明确的答案。这段代码是否回到了服务中?(非常怀疑它,因为它有 flash.message 指向控制器执行所有这些操作。)尝试将其变成服务和事务然后也许你可以考虑删除 withNewTransaction 调用。

    您可以在此处阅读有关创建的错误的更多信息: Grails - DuplicateKeyException 点评评论: “好吧,当您手动初始化具有 id 或唯一属性的类 new ClassD 时,当会话中已经存在另一个时,就会出现这个问题。因此,您应该首先尝试获取该实体(这就是 findOrCreateWhere 所做的,但是如果您使用您需要使用的 id 获取),然后使用找到的实例或创建一个新的实例进行更新”

    Hibernate Error: a different object with the same identifier value was already associated with the session

    您的代码已整理并从服务中运行:(问题可能会消失)因为我还清理了您正在执行的重复发现:

    class TestService {
    
        static transactional=true
    
        def saveRecord()  {
            def results=[]
            new ExcelBuilder(excelFile.inputStream).eachLine([labels: true, sheet: 0]) {
                if (cell(0)) {
                    def nameA = cell(0)
                    def nameB = cell(1)
                    def code = cell(2)
                    def designation = cell(3)
    
                    def a = Chapitre.findByNom(nameA)
                    def b = Rubrique.where{nom == nameB && chapitre == a}
                    def c = Object.where{rubrique == b && c == code && d == designation}
    
                    if (!code||!nameA||!nameB||!a||!b||!c) {
                        results << "Error saving ${nameA} ${nameB} ${code}"
                    } else {
                        //boolean isNew = false
                        def object = Object.findOrSaveWhere(code:code)
                        if(object) { 
                            if (!object.designation) {
                                rowCount++
                                results << "Record ${object} has no designation ? new Record?"
                            }
                            object.rubrique = b
                            object.d = designation ?: ''
                            object.save()
                            results << "Record ${object.id} is saved"
                        } else {
                            /*
                             *  Could not save or find code:code now create a new object:
                             *  object = new Object(code:code, rubrique:rubrique: d: designation ?: '').save()
                             *  
                             */
                        }   
                    }
                }
                currentLine++
            }
            results <<  "${rowCount} ligne create or update"
            return results
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 2012-06-11
      • 2013-11-26
      • 2015-05-13
      • 2012-05-06
      相关资源
      最近更新 更多