快乐着飞舞着

---Nothing to do is doing nothing
随笔 - 93, 文章 - 5, 评论 - 56, 引用 - 0
数据加载中……

一个关于 CMP entity bean 的诡异问题

create table results(sno number(8references students on delete cascade,
                      cno 
number(5references courses on delete cascade,
                      primary key(sno,cno));

 

建立一个CMP Entity Bean
在这个bean 中 有如下的方法:

 

 1  /**
 2   *
 3   * <!-- begin-user-doc -->
 4   * The  ejbCreate method.
 5   * <!-- end-user-doc -->
 6   *
 7   * <!-- begin-xdoclet-definition --> 
 8   * @ejb.create-method 
 9   * <!-- end-xdoclet-definition --> 
10   * @generated
11   */

12  public resultPK ejbCreate(Integer sno,Integer cno) throws javax.ejb.CreateException {
13    // EJB 2.0 spec says return null for CMP ejbCreate methods.
14    // TODO: YOU MUST INITIALIZE THE FIELDS FOR THE BEAN HERE. 
15    // setMyField("Something"); 
16    // begin-user-code
17    // this.setCno(cno);
18    // this.setSno(sno);
19    return new resultPK(sno,cno);
20    // end-user-code
21  }

22
23  /**
24   * <!-- begin-user-doc -->
25   * The container invokes this method immediately after it calls ejbCreate.
26   * <!-- end-user-doc -->
27   * 
28   * @generated
29   */

30  public void ejbPostCreate(Integer sno,Integer cno) throws javax.ejb.CreateException {
31    // begin-user-code
32      try{
33        Hashtable props = new Hashtable();
34
35        props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
36                "weblogic.jndi.WLInitialContextFactory");
37        props.put(InitialContext.PROVIDER_URL, "t3://127.0.0.1:7001");
38
39        InitialContext initialContext = new InitialContext(props);
40        csc.studentLocalHome studentlocalhome=(studentLocalHome)initialContext.lookup(csc.studentLocalHome.JNDI_NAME);
41        this.setStudent(studentlocalhome.findByPrimaryKey(sno));
42        csc.courseLocalHome courselocalhome=(courseLocalHome)initialContext.lookup(csc.courseLocalHome.JNDI_NAME);
43        this.setCourse(courselocalhome.findByPrimaryKey(cno));
44
45    }
catch(Exception e){
46        e.printStackTrace();
47    }
System.out.println("ejb postCreate");
48    // end-user-code
49  }

50

 

 

 

 1  /**
 2   * @ejb.relation 
 3   * name = "resultTOstudent"
 4   * role-name = "result_student"
 5   * target-ejb = "student"
 6   * target-multiple = "yes"
 7   * 
 8   * @weblogic.column-map 
 9   * foreign-key-column = "sno"
10   * key-column = "sno"
11   * @ejb.interface-method 
12   * view-type = "local"
13   */

14 public abstract studentLocal getStudent();
15 /**
16  * @ejb.relation 
17  * name = "resultTOCourse"
18  * role-name = "result_course"
19  * target-ejb = "course"
20  * target-multiple = "yes"
21  * 
22  * @weblogic.column-map 
23  * foreign-key-column = "cno"
24  * key-column = "cno"
25  * @ejb.interface-method 
26  * view-type = "local"
27  */

28 public abstract courseLocal getCourse();

 

在客户程序中调用时

 

 

csc.result myBean = getHome().create(new Integer(Integer.parseInt("02012001")),new Integer(18));

 

有如下的错误:

javax.ejb.CreateException: [EJB:010148]In EJB 'result', the primary key field 'cno' was not set during ejbCreate. All primary key fields must be initialized during ejbCreate.

如果把 ejbCreate改成

 

 1  public resultPK ejbCreate(Integer sno,Integer cno) throws javax.ejb.CreateException {
 2    // EJB 2.0 spec says return null for CMP ejbCreate methods.
 3    // TODO: YOU MUST INITIALIZE THE FIELDS FOR THE BEAN HERE. 
 4    // setMyField("Something"); 
 5    // begin-user-code
 6      this.setCno(cno);
 7      this.setSno(sno);
 8    return new resultPK(sno,cno);
 9    // end-user-code
10  }

 

有下面的错误:

The setXXX method for a cmr-field that is mapped to a primary key may not be called. The cmr-field is read-only.

特别的是 在第二种情况下 create 方法虽然有错误 但是数据仍然插入了数据库,第一种情况下程序自然终止。

posted on 2005-07-01 18:38 快乐着飞舞着 阅读(826) 评论(3)  编辑 收藏 引用 所属分类: Java

评论

# re: 一个关于 CMP entity bean 的诡异问题  回复  更多评论   

该 问题解决了
原来 只要 把 ejbPostCreate ()
中 的 部分去掉就 好了
2005-08-04 21:13 | 快乐着飞舞着

# re: 一个关于 CMP entity bean 的诡异问题  回复  更多评论   

hey i dont know about the above fileds which r written in chinese........but all i wanna share is this page helped me....kudos for the authors......
2006-04-07 18:57 | vj24x7@yahoo.com

# re: 一个关于 CMP entity bean 的诡异问题  回复  更多评论   

I'm so glad that it can help u.
2006-04-08 21:29 | 快乐着飞舞着
只有注册用户登录后才能发表评论。

Clicky