有关保存在GORM的更多信息

时间:2014-03-06 12:13:18

标签: java grails gorm

我有一个简单的代码块,返回false

PromoDebit promoDebit = new PromoDebit();
            promoDebit.promoCode=promoCode;
            promoDebit.userId=userId;
            promoDebit.countUsages=countUsages;
            promoDebit.endDate=endDate;
            promoDebit.startDate=startDate;
            promoDebit.status=1;
            promoDebit.calcValue=Float.parseFloat(p.getProperty("promoPercent"));
             if(promoDebit.save(flush: true)){
                 log.info "GOOD!"
            } else {
                 log.info "BAD!"
             }

如何获得有关GORM.save失败的更多信息?即使我在DataSource.groovy

中打开logSql,Stdrout日志也显示无法显示

2 个答案:

答案 0 :(得分:4)

首先,确保正确配置了Log4j。

此处http://grails.org/doc/2.3.1/guide/conf.html#logging

而且,如果您想了解更多关于GORM.save的详细信息,请

        PromoDebit promoDebit = new PromoDebit();
        promoDebit.promoCode=promoCode;
        promoDebit.userId=userId;
        promoDebit.countUsages=countUsages;
        promoDebit.endDate=endDate;
        promoDebit.startDate=startDate;
        promoDebit.status=1;
        promoDebit.calcValue=Float.parseFloat(p.getProperty("promoPercent"));

然后,

if (!promoDebit.save()) {
   log.warn myDomainObj.errors.allErrors.join(' \n') 
   //each error is an instance of  org.springframework.validation.FieldError    
}

而且,我喜欢这个

if (!promoDebit.save()) {
    promoDebit.errors.each {
        println it
    }
}

如果要为每个域类抛出异常,只需设置grails.gorm.failOnError to true in grails-app/conf/Config.groovy

即可

或简单

promoDebit.save(failOnError:true)

干杯!

答案 1 :(得分:1)

log.warn "error occurred by saving: $promoDebit.errors"

您将在日志中看到验证失败。如果某些SQL约束被破坏,您将得到一个完整的Exception,如DataIntegrityException