DuplicateKeyException during a save

M

melodyBibi

Guest
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.

Code:
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 ?

I Thank You in advance
 

ScottGoaws

New member
Greetings from Peacequest Education!

Be the first to enrol for Spoken English or IELTS/ Life coach online classes at PEACEQUEST EDUCATION
Stay safe and overcome your English fever concern with PEACEQUEST!

Courses - AVIATION/HOSPITALITY/RETAIL/TRAVEL, CELPIP,TOEFL,PTE,IELTS,SAT,BASIC ENGLISH/ADVANCE ENGLISH/PERSONALITY GROOMING SESSION/INTERVIEW TECHNIQUES/VOICE & ACCENT/BUSINESS ENGLISH on attractive 50% discounts.

PEACEQUEST EDUCATION provides ONLINE CLASSES WHICH CAN BE AVAILED SITTING AT YOUR PLACE,YOU ARE JUST AT A CLICK AWAY TO JOIN WHICHEVER BAND YOU LOOKING FOR:

Unprecedented sessions with alternate days MOCK TESTS,FREE ONLINE PRACTICE SESSIONS,VOCABULARY, IELTS PRELIMINARY SESSIONS,

HURRY UP TO GRAB YOUR DESIRED SCORE!

Regular classes all 7 days a week

Timings:6am to 9pm

PEACEQUEST EDUCATION New Delhi
Contact 9999828122/7982065847
Website: www.peacequesteducation.org

Best regards
Team Peacequest Education
 
Top