如何获得Hibernate的数据库底层异常

来源:互联网 发布:百度竞价推广优化 编辑:程序博客网 时间:2024/06/06 02:21


 /**
  * 场所类型的增加或修改
  * @param oldPlaceTypeCode -1表示是添加,其他表示修改
  * @param newDdPlacetype  -变更的场所类型对象
  * @return
  */
 public PageQueryOutput saveDdPlacetype(Long oldPlaceTypeCode,DdPlacetype newDdPlacetype){
  PageQueryOutput output=new PageQueryOutput();       
  try { 
      DdPlacetype pt=this.orgLogic.saveDdPlacetype(oldPlaceTypeCode, newDdPlacetype);
      output.getMap().put("saveobj", pt);
  }catch (Exception ex) {
      output.setResult(ResultCode.Failed);
   
     HibernateSQLError sqlErro=new HibernateSQLError();
     sqlErro.showTraces(ex);
     output.setDesc("保存场所类型失败,原因:"+sqlErro.errorMsg);

   
     log.error(output.getDesc());
  }
  return output;
 }
 
 /**
  *
  * 如果要获取底层的异常,需要通过e.getCause()递归获取,直到e.getCause()==null为止,才可以获取到底层的异常。
  *
  */
 private class HibernateSQLError{ 
  public String  errorMsg="";
  private  String showTraces(Throwable t) { 
       Throwable next = t.getCause(); 
     if (next == null) {
        errorMsg=t.getMessage();//底层SQl异常
      } else { 
         showTraces(next); 
      }
     return t.getLocalizedMessage();
  } 
 }

0 0