Catch() 不捕获异常

时间:2021-07-08 23:52:47

标签: java spring spring-boot hibernate

我正在应用一个实现 Spring 验证的开发,但我遇到的问题是,当我插入一个具有 Codigo 属性的对象时,它会抛出一个错误,但我无法捕捉到它使用 try-catch。

我期望当它因为空值而失败时,我可以获得该错误,抛出 try-catch()。

对象

@Column( name = "codigo" , unique = true , nullable = false , length = 255 )
@UniqueElements( message = "El codigo debe ser unico.")
@Length( max = 255 , message = "El codigo debe ser menos a 255 caracteres")
@NotNull( message = "El codigo es requerido.")
private String codigo;


@Column( name = "motivo" , unique = false , nullable = false , length = 255 )
@Length( max = 255 , message = "El motivo debe ser menos a 255 caracteres")
@NotNull( message = "El motivo es requerido.")
private String motivo;


@Column( name = "active" , unique = false , nullable = false )
@NotNull( message = "El estado es requerido.")
private Boolean active;

服务

 @Transactional( rollbackFor = BusinessLayerException.class )
 public String insertMotivoBaja( MotivoBajaTO motivoTO ) throws 
 BusinessLayerException {

try {
    
    MotivoBaja motivo = super.getPersistentObject( motivoTO , MotivoBajaTOT.class );
    return super.add( motivo );
    
  }catch (Exception e) {
    
      this.logger.error( e.getMessage() );
    
      if( e.getMessage().toLowerCase().startsWith("El".toLowerCase() ) ) {
          throw new BusinessLayerException(  e.getMessage());
      }
    
       throw new BusinessLayerException( "Error al dar de alta el motivo. Por Favor intente nuevamente." );    
    }

}

错误

<块引用>

org.springframework.transaction.TransactionSystemException:无法提交 JPA 事务;嵌套异常是 javax.persistence.RollbackException:提交事务时出错

<块引用>

引起:javax.validation.UnexpectedTypeException:HV000030:找不到约束'org.hibernate.validator.constraints.UniqueElements'验证类型'java.lang.String'的验证器。检查“codigo”的配置

请求(正文)

 {
   "codigo":null,
   "motivo":"No quiere ser mas cliente",
   "active": true 
 }  

1 个答案:

答案 0 :(得分:1)

来自documentation

<块引用>

验证所提供的集合中的每个对象都是唯一的

String 不是 Collection,抛出的异常不会在 try 方法内部发生,但在它之前,这就是它没有被捕获的原因

相关问题