0 Replies Latest reply on Oct 11, 2004 4:51 AM by Ivanisevic Yiu

    Help, CMR deployed, but not link together

    Ivanisevic Yiu Newbie

      I got a problem on the relation 1-many MCP/CMR

      there is not problem when i was deploy the EJB

      when the client get the teacher from the student or get the students from teacher. it return 0 size collection from teacher and null teacher from student..can anybody help me??????????

      beside the database connection is ok since it can store record that I have made.
      ----------------
      database

      teacher
      id : string (pk)
      name : string

      student
      id : string (pk)
      name : string
      teacherId : string (fk)

      TeacherBean

      /*
       * Created on 2004/10/11
       *
       * TODO To change the template for this generated file go to
       * Window - Preferences - Java - Code Style - Code Templates
       */
      package school;
      
      import java.rmi.RemoteException;
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.Iterator;
      
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;
      import javax.ejb.RemoveException;
      
      /**
       * @author ivanisevic
       *
       * TODO To change the template for this generated type comment go to
       * Window - Preferences - Java - Code Style - Code Templates
       */
      public abstract class TeacherBean implements EntityBean{
      
       public EntityContext context;
      
       //access method for cmp
       public abstract String getId();
       public abstract void setId(String id);
      
       public abstract String getName();
       public abstract void setName(String name);
      
       //access method for cmr
       public abstract Collection getStudent();
       public abstract void setStudent(Collection student);
      
       public ArrayList getStudentList() {
       try{
       ArrayList list = new ArrayList();
       Iterator c = getStudent().iterator();
       while (c.hasNext()) {
       list.add((Student)c.next());
       }
       return list;
       }catch(Exception e){return new ArrayList();}
       }
      
       public String ejbCreate(String id, String name) throws CreateException{
       return create(id, name);
       }
      
       private String create(String id,String name) throws CreateException {
       setId(id);
       setName(name);
       return id;
       }
      
       public void ejbPostCreate(String id , String name) throws CreateException{}
      
       public String ejbCreate(String id) throws CreateException{
       return create(id, "");
       }
      
       public void ejbPostCreate(String id) throws CreateException{}
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbActivate()
       */
       public void ejbActivate() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbLoad()
       */
       public void ejbLoad() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbPassivate()
       */
       public void ejbPassivate() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbRemove()
       */
       public void ejbRemove() throws RemoveException, EJBException,
       RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbStore()
       */
       public void ejbStore() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#unsetEntityContext()
       */
       public void unsetEntityContext() throws EJBException, RemoteException {
       context = null;
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
       */
       public void setEntityContext(EntityContext ctx) throws EJBException,RemoteException {
       context = ctx;
       }
      
      
      
      }
      
      


      StudentBean
      /*
       * Created on 2004/10/11
       *
       * TODO To change the template for this generated file go to
       * Window - Preferences - Java - Code Style - Code Templates
       */
      package school;
      
      import java.rmi.RemoteException;
      
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;
      import javax.ejb.RemoveException;
      
      /**
       * @author ivanisevic
       *
       * TODO To change the template for this generated type comment go to
       * Window - Preferences - Java - Code Style - Code Templates
       */
      public abstract class StudentBean implements EntityBean {
      
       private EntityContext context;
      
       //access method for cmp
       public abstract String getId();
       public abstract void setId(String id);
      
       public abstract String getName();
       public abstract void setName(String name);
      
       public abstract String getTeacherId();
       public abstract void setTeacherId(String teacherId);
      
       //access method for cmr
       public abstract Teacher getTeacher();
       public abstract void setTeacher(Teacher teacher);
      
      
       public String ejbCreate(String id, String name) throws CreateException{
       return create(id, name,"");
       }
      
       private String create(String id,String name, String teacherId) throws CreateException {
       setId(id);
       setName(name);
       setTeacherId(teacherId);
       return id;
       }
      
       public void ejbPostCreate(String id , String name) throws CreateException{}
      
       public String ejbCreate(String id) throws CreateException{
       return create(id, "","");
       }
      
       public void ejbPostCreate(String id) throws CreateException{}
      
       public String ejbCreate(String id, String name, String teacherId) throws CreateException{
       return create(id, name, teacherId);
       }
      
       public void ejbPostCreate(String id, String name, String teacherId) throws CreateException{}
      
      
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbActivate()
       */
       public void ejbActivate() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbLoad()
       */
       public void ejbLoad() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbPassivate()
       */
       public void ejbPassivate() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbRemove()
       */
       public void ejbRemove() throws RemoveException, EJBException,
       RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#ejbStore()
       */
       public void ejbStore() throws EJBException, RemoteException {
       // TODO Auto-generated method stub
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#unsetEntityContext()
       */
       public void unsetEntityContext() throws EJBException, RemoteException {
       context = null;
      
       }
      
       /* (non-Javadoc)
       * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
       */
       public void setEntityContext(EntityContext ctx) throws EJBException,RemoteException {
       context = ctx;
       }
      
      }
      
      


      ejb-jar.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" version="2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
      <display-name xml:lang="zh">school</display-name>
      <enterprise-beans>
      <entity>
      <ejb-name>TeacherBean</ejb-name>
      <local-home>school.LocalTeacherHome</local-home>
      <local>school.LocalTeacher</local>
      <home>school.TeacherHome</home>
      <remote>school.Teacher</remote>
      <ejb-class>school.TeacherBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>false</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>TeacherBean</abstract-schema-name>
      <cmp-field>
      <description xml:lang="zh">no description</description>
      <field-name>name</field-name>
      </cmp-field>
      <cmp-field>
      <description xml:lang="zh">no description</description>
      <field-name>id</field-name>
      </cmp-field>
      <primkey-field>id</primkey-field>
      <security-identity>
      <use-caller-identity/>
      </security-identity>
      </entity>
      <entity>
      <ejb-name>StudentBean</ejb-name>
      <local-home>school.LocalStudentHome</local-home>
      <local>school.LocalStudent</local>
      <home>school.StudentHome</home>
      <remote>school.Student</remote>
      <ejb-class>school.StudentBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>false</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>StudentBean</abstract-schema-name>
      <cmp-field>
      <description xml:lang="zh">no description</description>
      <field-name>teacherId</field-name>
      </cmp-field>
      <cmp-field>
      <description xml:lang="zh">no description</description>
      <field-name>name</field-name>
      </cmp-field>
      <cmp-field>
      <description xml:lang="zh">no description</description>
      <field-name>id</field-name>
      </cmp-field>
      <primkey-field>id</primkey-field>
      <security-identity>
      <use-caller-identity/>
      </security-identity>
      </entity>
      </enterprise-beans>
      <relationships>
      <ejb-relation>
      <ejb-relation-name>Student-Teacher</ejb-relation-name>
      <ejb-relationship-role>
      <multiplicity>One</multiplicity>
      <relationship-role-source>
      <ejb-name>TeacherBean</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>student</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <multiplicity>Many</multiplicity>
      <relationship-role-source>
      <ejb-name>StudentBean</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>teacher</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>
      </relationships>
      </ejb-jar>
      
      


      jbosscmp-jdbc.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 3.2//EN" "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_2.dtd">
      
      <jbosscmp-jdbc>
       <defaults>
       <datasource>java:/MySqlDS</datasource>
       <datasource-mapping>mySQL</datasource-mapping>
       </defaults>
      
       <enterprise-beans>
      
       <entity>
       <ejb-name>TeacherBean</ejb-name>
       <create-table>true</create-table>
       <remove-table>true</remove-table>
       <table-name>teacher</table-name>
       <cmp-field>
       <field-name>id</field-name>
       <column-name>id</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>name</field-name>
       <column-name>name</column-name>
       </cmp-field>
      
       </entity>
      
       <entity>
       <ejb-name>StudentBean</ejb-name>
       <create-table>true</create-table>
       <remove-table>true</remove-table>
       <table-name>student</table-name>
       <cmp-field>
       <field-name>id</field-name>
       <column-name>id</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>name</field-name>
       <column-name>name</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>teacherId</field-name>
       <column-name>teacherId</column-name>
       </cmp-field>
       </entity>
       </enterprise-beans>
      
       <relationships>
      <ejb-relation>
      <ejb-relation-name>Student-Teacher</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TeacherBean_student</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>id</field-name>
      <column-name>teacherId</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>StudentBean_teacher</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>
       </relationships>
      </jbosscmp-jdbc>