EJB容器认为异常有两种方式 -JAVA EE - EJB/CDI/JPA:异常处理
应用程序异常 - 如果违反业务规则或在执行业务逻辑出现异常。
系统异常 - 任何异常,这不是由业务逻辑或业务代码引起的。 RuntimeException,RemoteException是SystemException。例如,在EJB查找期间发生错误。 RuntimeException,RemoteException是SystemException。
- >这是否意味着我需要使用checked异常我bussines逻辑是什么?喜欢这个 ?
private void checkConstraints(Object object) throws ValidationException{
Set<ConstraintViolation<Object>> constraintsAdress = this.getValidator().validate(object);
if(!constraintsAdress.isEmpty()){
String fullErrorConstraint = "";
for (ConstraintViolation<Object> constraint : constraintsAdress) {
fullErrorConstraint = fullErrorConstraint + constraint.getMessage() + "\n";
}
throw new ValidationException(fullErrorConstraint);
}
}
@Override
public long addCafe(Cafe cafe) throws ValidationException, DBException{
this.checkConstraints(cafe.getAddress());
for(FootballMatch footballMatch: cafe.getNextMatchesToWatch()){
this.checkConstraints(footballMatch);
}
this.checkConstraints(cafe);
this.getManager().persist(cafe);
return cafe.getCafeID();
}
但是......
应用程序异常不会自动导致除非ApplicationException的注释应用到异常类,并与回滚元素值true指定标记事务回滚...
我不完全理解它了...... 这是个好主意,用:
- @ApplicationException(rollback = true)?
- 你可以使用这一个,然后也作出unchecked异常?
THX提前 干杯 汤姆