create table results(sno number(8) references students on delete cascade,
cno number(5) references 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 方法虽然有错误 但是数据仍然插入了数据库,第一种情况下程序自然终止。