<%@ page contentType="text/html;charset=gb2312" %>
BEA Logo
销售咨询热线:800-810-8878提交采购需求订阅购买下载全球站点
SolutionsSolutions Products Services Support Developers Partners Customers About BEA
专业咨询服务
客户支持服务
服务导航
活动与新闻
技术资源
AskBEA
常见技术问题
BEA产品文档
BEA新闻组
dev2dev 中文站点
dev2dev 英文站点
其它有用资源
联系我们
培训服务
最新培训
服务
 

当使用CMR (CMP Relations)时如遇到以下exception:
"javax.ejb.EJBException: When a cmp-field and a cmr-field (relationship) are mapped to the same column, the setXXX method for the cmp-field may not be called. The cmp-field is read-only."

原因:
当你希望在CMP中定义一个Many-to-One Relationships指向另一个CMP的PK字段时,这个cmp field不能使用setXXX方法更新。在WLS和EJB 2.0规范的10.3.1部分已经提到了这一点。请参考http://e-docs.bea.com/wls/docs70/faq/ejb.html中内容。

解决:
1)在ejbCreate方法中设置除关联字段以外的其它所有字段值
2)通过relations CMP filed的值用ejbPostCreate方法找到relations CMP ejb
3) 用找到的CMP ejb去设置关联字段的值

以下代码是在相关entity EJB中调用ejbPostCreate方法设置字段值的例子:

  public void ejbPostCreate(String id, String name, String type) throws CreateException {
  ……
  try{
  Context ctx = new InitialContext();
  TypeInfoHome tih = (TypeInfoHome)ctx.lookup("TypeInfo");
  //This is the relations CMP EJB home
  ……
  TypeInfo ti= tih.findByPrimaryKey("1");
  // find the relations CMP EJB object.
  ……
  this.setTypeInfo(ti);
  }catch(Exception e){
  System.out.println("_______________________________________");
  e.printStackTrace();
  }
  ……}