DuplicateKeyException during a save

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

DuplicateKeyException during a save

Mélody Scholtes

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) {
.messages << "error"
} else if (!Chapitre.findByNom(nameA)) {
.messages << "error"
} else if ( Rubrique.where{nom == nameB && chapitre == a}.list().size() == 0) {
.messages << "error"
} else if(Object.where{rubrique == Rubrique.findByNom(nameB) && c == code && d == designation}.count() > 0){
.messages << "error"
} else {
def b = Rubrique.findByNom(nameB)
def isNew = false;

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

if(object.designation == null)
= true;
object.rubrique = b
object.d= (designation == null)?"":designation
//                                try    {
+= object.save()? 1 : 0
//                                } catch(ValidationException) {
//                                      if(isNew)
//                                          rowCount++;
//                                      log.info("ErreuRRRRRRRRrrrRRrrRrRRrrrrRrrrRrrrRrrrr")
//                                }
.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 ?

You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/210774ca-26f9-4ffb-9e15-f36fbe6826d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.