从 excel 文件导入后出现多个 GORM ValidationException
Multiple GORM ValidationException after import from excel file
我正在处理一个错误。我有一个导入到应用程序中的 excel 文件。
该文件包含核心域对象(动物园,见下文)的信息。每行有两个单元格,名称为字符串值,动物数量为整数值。
如果我在这个 excel 文件中有一个无效字段,它将显示一条错误消息,例如 "Error on column X on cell Y. Value is "someValue" invalid"。假设我们有一个包含 250 行的 excel 文件。如果第 200 行出现无效字段错误,它之后的每一行都会抛出以下错误
Validation error whilst flushing entity [com.rolotec.ost.Zoo]:
- Field error in object 'com.rolotec.ost.Zoo' on field 'ceo': rejected value [null]
如果我查看数据库,对象在 ceo 字段上不为空。
这里是域对象。
class Zoo{
String name;
int animalsInTheZoo;
CEO ceo;
hasMany=[ zooEnclosures : ZooEnclosures]
constraints=[//some constraints]
}
class CEO{
Date ceoTillDate;
Person person;
constraints=[//some constraints]
}
class Person{
String name;
int age;
constraints=[//some constraints]
}
这是导入服务
class ImportService{
importExcel(){
String errorMsg="";
Zoo.withTransaction { status->
try{
//some other operations
for(int rowNum = excelFile.firstRow; rowNum<=excelFile.lastRow; rowNum++) {
try {
importRow(row);
} catch (Exception e) {
//doSomething
}
} //end for-loop
if (errorMsg != "") {
status.setRollback();
}
} catch(Exception e){
//doSomething with e
status.setRollback();
}
}
}
importRow(Row row){
String name = row.getCell(1).stringCellValue;
Zoo.findAllByName(name).each{
try{
//reads every cell in Excel data and validates the field
it.save() //error occures here in the validation
} catch(Exception e){
//doSomething with e
}
}
}
如果我检查该对象,它将具有 it.ceo.name 的值,但 it.ceo 中的其余值将为空。也将是 it.ceo.person = null。在数据库中有属性的所有值。
编辑: 如果 excel 文件中没有无效字段,则没有 ValidationException。一切都会好起来的。
编辑 2: 在 importExcel() 方法中添加了 for 循环。
问题是,Zoo.withTransaction
闭包内部有一个 for 循环,它循环遍历 excel 文件中的所有行。对于解决方案,我们将 .withTransaction
移到 for 循环中,问题就消失了。
ImportService 中的代码现在如下所示:
importExcel(){
String errorMsg="";
//some operations
for(int rowNum = excelFile.firstRow; rowNum<=excelFile.lastRow; rowNum++){
Zoo.withTransaction{status ->
//some operations
try{
importRow(row)
}
catch(Exception e){
//do something with exception
status.setRollbackOnly()
}
}
}
}
我正在处理一个错误。我有一个导入到应用程序中的 excel 文件。 该文件包含核心域对象(动物园,见下文)的信息。每行有两个单元格,名称为字符串值,动物数量为整数值。
如果我在这个 excel 文件中有一个无效字段,它将显示一条错误消息,例如 "Error on column X on cell Y. Value is "someValue" invalid"。假设我们有一个包含 250 行的 excel 文件。如果第 200 行出现无效字段错误,它之后的每一行都会抛出以下错误
Validation error whilst flushing entity [com.rolotec.ost.Zoo]:
- Field error in object 'com.rolotec.ost.Zoo' on field 'ceo': rejected value [null]
如果我查看数据库,对象在 ceo 字段上不为空。
这里是域对象。
class Zoo{
String name;
int animalsInTheZoo;
CEO ceo;
hasMany=[ zooEnclosures : ZooEnclosures]
constraints=[//some constraints]
}
class CEO{
Date ceoTillDate;
Person person;
constraints=[//some constraints]
}
class Person{
String name;
int age;
constraints=[//some constraints]
}
这是导入服务
class ImportService{
importExcel(){
String errorMsg="";
Zoo.withTransaction { status->
try{
//some other operations
for(int rowNum = excelFile.firstRow; rowNum<=excelFile.lastRow; rowNum++) {
try {
importRow(row);
} catch (Exception e) {
//doSomething
}
} //end for-loop
if (errorMsg != "") {
status.setRollback();
}
} catch(Exception e){
//doSomething with e
status.setRollback();
}
}
}
importRow(Row row){
String name = row.getCell(1).stringCellValue;
Zoo.findAllByName(name).each{
try{
//reads every cell in Excel data and validates the field
it.save() //error occures here in the validation
} catch(Exception e){
//doSomething with e
}
}
}
如果我检查该对象,它将具有 it.ceo.name 的值,但 it.ceo 中的其余值将为空。也将是 it.ceo.person = null。在数据库中有属性的所有值。
编辑: 如果 excel 文件中没有无效字段,则没有 ValidationException。一切都会好起来的。
编辑 2: 在 importExcel() 方法中添加了 for 循环。
问题是,Zoo.withTransaction
闭包内部有一个 for 循环,它循环遍历 excel 文件中的所有行。对于解决方案,我们将 .withTransaction
移到 for 循环中,问题就消失了。
ImportService 中的代码现在如下所示:
importExcel(){
String errorMsg="";
//some operations
for(int rowNum = excelFile.firstRow; rowNum<=excelFile.lastRow; rowNum++){
Zoo.withTransaction{status ->
//some operations
try{
importRow(row)
}
catch(Exception e){
//do something with exception
status.setRollbackOnly()
}
}
}
}