Spring GRAILS-GORM:保存新对象时出现DuplicateKeyException
我使用GORM从excel文件中备份数据库中的事件Spring GRAILS-GORM:保存新对象时出现DuplicateKeyException,spring,hibernate,grails,gorm,Spring,Hibernate,Grails,Gorm,我使用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
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"
有问题的对象的注册是有效的,但该错误在文件路径之外引发
当我取消对try-and-catch的注释时,我绕过了错误,因此在数据库中创建了每个文件的所有副本
因此,我找到了一种解决我的担忧的方法,但我觉得它不是很清晰,我来找你们,试图了解我的问题。没有进一步的信息,很难给出任何明确的答案。这段代码是否返回到服务中?非常怀疑,因为它有flash.message指向控制器执行所有操作。试着把它变成一个服务和事务性的,然后也许你可以考虑用newtransaction调用删除它 您可以阅读有关此处创建的错误的更多信息: 审查意见: 当您手动初始化一个具有id或唯一属性的类new ClassD时,当会话中已经存在另一个类时,就会出现这个问题。因此,您应该首先尝试获取该实体findOrCreateWhere就是这样做的,但是如果您使用id,则需要使用get,然后使用找到的实例或为更新创建一个新实例 您整理了代码并从服务运行:问题可能会消失,因为我也清理了您正在执行的重复查找:
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
}
}
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
}
}