OAF学习笔记-JAVA Entity Object 之 Update/Validate data(深入EO系列)

本文介绍在更新数据前如何进行有效的字段级和记录级校验,包括EO的set()方法及validateEntity()方法的具体应用,并强调正确的异常处理方式。

Update/Validate data(更新/校验数据)

Update数据之前,我们往往要对待更新的记录进行有效性的校验,校验级别包括:

Attribute Level Validation :字段级的校验

Entity Level Validation : 记录级的校验

字段级别的校验一般情况下写在EOset()方法中,根据传入的value来决定是执行setAttributeInternal() 还是throw Exception.(代码应写在setAttributeInternal()之前)

例如你要限制ItemPrice 字段的值不能超过100(当然也可以通过item的属性来设置). 伪码如下:

 Public void setItemPrice( Number value)

 {

Number maxvalue = new Number(100);

If (value.compareTo(100) >0)

{ throw  new Exception(“ Error: Item Price large then 100!”);}

else

 { setAttributeInternal(ITEMPRICE, value);}

}

当校验涉及多个Attribute,你就不能使用字段级的校验了,需使用记录级的校验,Entity Level Validation. 此校验在validateEntity()方法中实现.校验代码须写在   super.validateEntity() 之后.

例如如果一个订单的状态为CLOSE, 则不允许更改OrderPrice. 伪码如下:

Public void   validateEntity()

{

   super.validateEntity();

   String status = getOrderStatus();

If (“CLOSE”.equals(status))

{

 Number ldorderprice = (Number)getPostedAttribute(ORDERPRICE);

 Number neworderprice = (Number)getOrderPrice();

 If (oldorderprice.compareto(neworderprice)!=0)

 {

  throw new Exception(“Error: Order Price can not be edited!”);

 }

}

}

备注: getPostedAttribute()方法用来取得字段未修改前的值.

另外, User’s Guide里介绍了多EO之间的校验.这留待后面再研究吧.

不要试图在EOValidation 里执行rollback() 方法或clearcache() 方法. 当出现校验失败时,有下面两种做法:

Bad Method:

public void validateEntity()

{

 ….

 transaction.rollback()

 throw new OAException….

}

Right Method:

public void validateEntity()

{

 throw new OAException…

}

In AM Module :

Try

{  transaction.commit();

}

Catch ( OAException ex)

{transaction.rollback();}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10359218/viewspace-677447/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10359218/viewspace-677447/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值