DuplicateKeyException during a save

111 views
Skip to first unread message

Mélody Scholtes

unread,
Oct 20, 2015, 5:16:49 AM10/20/15
to Grails Dev Discuss
Hello,

I am French so I took you to excuse me for my bad english.

I use GORM to back occurrences in a database, from an excel file.

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)
                           
                           
//request.getSession().removeAttribute("nuisanceType")
                           
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"




An update will break any worries, the course of the lines of file continu and database recording is effective.

However when it comes to inserting a new object I get an error :
" 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"

The registration of the object in question is effective , but the error raised off the path of the file.

When i uncommented the "try and catch" I bypasses the error and so all my copies of each file are created in the database .

I thus found a way around my worries, but I do not find it very clean and i come to you to try to understand my problem.

Any idea ?
Reply all
Reply to author
Forward
0 new messages