有时候业务逻辑会需要根据某个对象的主键来判断这个对象是否在数据库中存在,从而进行不同的逻辑处理。下面是一段示例代码。
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);
}
根据PrimaryKey判断某记录是否在数据库表中存在
