2015-10-20 117 views
0

我使用GORM从excel文件中删除数据库中的事件。保存一个新对象GRAILS - GORM:DuplicateKeyException

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 

的对象有问题的注册是有效的,但上调过错误的文件的路径。

当我取消注释trycatch我绕过错误,所以我的每个文件的副本都创建在数据库中。

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

回答

0

没有进一步的信息很难给出任何明确的答案。这段代码是否回到了服务中?(非常怀疑它,因为它有指向控制器的flash.message。)尝试把它变成服务和事务,然后你可以看看用NewTransaction调用去掉。

您可以在此处详细了解创建的错误: Grails - DuplicateKeyException 审查意见: “好了,当你手动初始化用一个id或独特的属性类新的D类,当有另一个已在会议上出现这个问题所以,你应该首先尝试获取实体(这是findOrCreateWhere做什么,但如果你使用一个id,你需要使用GET),然后使用中发现的实例或创建更新”

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 
    } 
}