根据PrimaryKey判断某记录是否在数据库表中存在

有时候业务逻辑会需要根据某个对象的主键来判断这个对象是否在数据库中存在,从而进行不同的逻辑处理。下面是一段示例代码。


            String strOrderId = "10001";
            OrderAccessBean abOrder = new OrderAccessBean();
            abOrder.setInitKey_orderId(strOrderId);
           
            try {
                abOrder.refreshCopyHelper();
               
                //this means order 10001 exist, you can continue to use abOrder
                System.out.print(abOrder.getDescription());
               
            } catch (ObjectNotFoundException e) {
                //this means order 10001 dose not exist, you need handle this case,
                //you can decided to throw exeption or ignore it because sometimes this is normal.
                System.out.print("order 10001 dose not exist");
            }

注意,只有ObjectNotFoundException 是可以cache后吞掉,其它的exception,如RemoteException,FinderException等等不能吞掉,需要转化成ECSystemException来抛出:

        try {

                //ejb related operations
                       ...
       

         } catch (javax.ejb.CreateException e) {
            throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, CLASS_NAME, methodName, ECMessageHelper
                    .generateMsgParms(e.toString()), e);
        } catch (javax.ejb.FinderException e) {
            throw new ECSystemException(ECMessage._ERR_FINDER_EXCEPTION, CLASS_NAME, methodName, ECMessageHelper
                    .generateMsgParms(e.toString()), e);
        } catch (javax.naming.NamingException e) {
            throw new ECSystemException(ECMessage._ERR_GENERIC, CLASS_NAME, methodName, ECMessageHelper
                    .generateMsgParms(e.toString()), e);
        } catch (java.rmi.RemoteException e) {
            throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, CLASS_NAME, methodName, ECMessageHelper
                    .generateMsgParms(e.toString()), e);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值