OAF做更新和删除之前都需要先查询,并且通过VO来操作,VO内部会调用EO,EO主要用于数据验证
按照BC4J的架构,通常是使用CO调用AM,在AM中操作VO以删除或跟新数据,但是当处理具有关联关系的数据时,例如删除一个PoHeader,那么则应该重写PoHeaderEO的remove()方法,以删除PoLine数据.
首先放AM删除/更新代码
- /**删除单个分类代码示例(官方版)*/
- OAViewObject vo = (OAViewObject)getCategoryVO1();
- CategoryVORowImpl row = null;
- int fetchedRowCount = vo.getFetchedRowCount();
- RowSetIterator deleteIter = vo.createRowSetIterator("deleteIter");
- if (fetchedRowCount > 0) {
- deleteIter.setRangeStart(0);
- deleteIter.setRangeSize(fetchedRowCount);
- for (int i = 0; i < fetchedRowCount; i++) {
- row = (CategoryVORowImpl)deleteIter.getRowAtRangeIndex(i);
- Number primaryKey = row.getCategoryId();
- if (primaryKey.compareTo(category_id) == 0) {
- row.remove();
- apply();
- break; // only one possible selected row in this case
- }
- }
- }
- // Always close the iterator when you're done.
- deleteIter.closeRowSetIterator();
使用这份代码有两个限制:
1.只能删除单行,如果去掉"break"来删除多行,迭代器返回会越界返回NULL值
2.如果数据没有绑定到WebBean控件,那么fetchedRowCount=0;删除不成功
仅仅是针对页面的单个绑定行,不知道ORACLE为什么还要把它作为示例代码。
为了消除第二条限制,需要做一点修改
- int RowCount = vo.getRowCount();
这样就可以以纯代码方式操作。
如果要删除多行,那么就不能用上面的代码了,我自己写了一段如下
- private void deleteComment(int article_id)
- {
- CommentVOImpl vo=(CommentVOImpl)getCommentVO1();
- CommentVORowImpl row = null;
- row=(CommentVORowImpl)vo.first();
- while(row!=null){
- Number primaryKey = row.getArticleId();
- if (primaryKey.compareTo(article_id) == 0) {
- }
- row.remove();
- if(vo.hasNext()) row=(CommentVORowImpl)vo.next();
- else break;
- }
- }
这段代码可以使用适用多种情况。
当然也可以针对具体情况再做点优化,例如,如果代码中用到的VO没有绑定页面控件,那么可以简单写成
- private void deleteFavorite(int article_id) {
- FavoriteVOImpl vo=(FavoriteVOImpl)getFavoriteVO1();
- vo.setWhereClause("FavoriteEO.article_id= :1");
- vo.setWhereClauseParams(null); // Always reset
- vo.setWhereClauseParam(0, new Number(article_id));
- vo.executeQuery();
- while(vo.hasNext())
- {
- Row row=vo.next();
- row.remove();
- }
- }
这里主要是涉及迭代器初始位置的问题。
下面贴一段关联删除的代码,首先要为两个关联的EO创建关系,然后把这段代码用于EO的remove
删除一个PoHeader,重写PoHeaderEO的remove()方法,以删除PoLine数据.还限制了可以删除的订单类型哦!
- public void remove()
- {
- String status = getStatusCode();
- if (("IN_PROCESS"Equals(status)) || ("REJECTED"Equals(status)))
- {
- // Note this is a good use of the header -> lines association since we
- // want to call remove( ) on each line.
- RowIterator linesIterator = getPurchaseOrderLineEO();
- if (linesIterator != null)
- {
- PurchaseOrderLineEOImpl line = null;
- while (linesIterator.hasNext())
- {
- line = (PurchaseOrderLineEOImpl)linesIterator.next();
- line.remove();
- }
- }
- super.remove(); // Must be called last in this case.
- }
- else
- {
- throw new OARowValException(OARowValException.TYP_ENTITY_OBJECT,
- getEntityDef().getFullName(),
- getPrimaryKey(),
- "ICX", // Message product short name
- "FWK_TBX_T_PO_NO_DELETE"); // Message name
- }
- } // end remove()
做更新的话,就把remove()换成setAttribute或setxxxx()就好了
这两个方法和删除都会改变VO状态为modify,如果不提交事务的话会被认为是脏数据的。