|
当使用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();
}
……}
|