2014-03-06 21 views
0

我有一個簡單的代碼塊更多信息,返回假關於保存在格姆

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更多信息失敗? Stdrout日誌顯示,即使我在DataSource.groovy中

回答

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
log.warn "error occurred by saving: $promoDebit.errors" 

打開logSql你會看到在日誌中驗證失敗nothning。如果某些SQL約束被破壞,您將得到一個全面的ExceptionDataIntegrityException