多對一(主鍵)關係,create問題

bclzcl發表於2004-06-01
我建立了兩個實體bean,關係為多對一,在多的實體create時,報錯:
"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部分已經提到了這一點。
網上有個文章給瞭解決方法:
解決:
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();
  }
  ……}


可例子中要設定的欄位直接給了一個數值:1,TypeInfo ti= tih.findByPrimaryKey("1");
但我是由客戶提供的數值,如下拉中選擇的,那麼提交時
我如何把另一個bean的鍵值傳給這個bean的ejbPostCreate呢,
各位仁兄能否給個具體的做法!!!!!!!!

相關文章