Compound key for an "association" EntityBean


//            +---------+  *                 *  +--------+
//            | Student | -----------+--------- | Course |
//            +---------+            |          +--------+
//                                   |
//                            +------------+
//                            | Enrollment |
//                            +------------+


package association;

public interface Student extends EJBObject {
  long   getId()   throws RemoteException;
  String getName() throws RemoteException;
}

public interface StudentHome extends EJBHome {
  public static final String JNDI_NAME = "StudentHome";

  Student    create( String name )       throws CreateException, RemoteException;
  Student    findByPrimaryKey( Long id ) throws FinderException, RemoteException;
  Collection findAll()                   throws FinderException, RemoteException;
}

public class StudentBean implements EntityBean {
  public Long   id;
  public String name;

  public long   getId()    { return id.longValue(); }
  public String getName()  { return name; }
  public Long   ejbCreate( String nam ) {
    try {
      if (pkGen == null)
        pkGen = new PKGenTranslationProxy();
      id = pkGen.getNextPKFor( StudentHome.JNDI_NAME );
    } catch (Exception ex) { ex.printStackTrace(); }
    System.out.println( "StudentBean.ejbCreate - " + nam );
    name = nam;
    return null;
  } ... }



public interface Course extends EJBObject { ... }
public interface CourseHome extends EJBHome { ... }
public class CourseBean implements EntityBean { ... }



public interface Enrollment extends EJBObject {
  String getSummary() throws RemoteException;
}

public interface EnrollmentHome extends EJBHome {
  public static final String JNDI_NAME = "EnrollmentHome";

  Enrollment create( Student s, Course c )       throws CreateException, RemoteException;
  Enrollment findByPrimaryKey( EnrollmentPK id ) throws FinderException, RemoteException;
  // Due to CMP mapping restrictions findByCourse() and findByStudent() must take a
  // primitive long because this is the column type in the database. 
  Collection findByCourse(  long activityId    ) throws FinderException, RemoteException;
  Collection findByStudent( long salespersonId ) throws FinderException, RemoteException;
}

public class EnrollmentBean implements EntityBean {
  public long student;
  public long course;

  public String getSummary() {
    return "student-" + student + ", course-" + course;
  }
  public EnrollmentPK ejbCreate( Student s, Course c ) {
    try {
      student = s.getId();
      course  = c.getId();
    } catch (java.rmi.RemoteException ex) { ex.printStackTrace(); }
    System.out.println( "EnrollmentBean.ejbCreate - " + student + ", " + course );
    return null;
  } ... }

public class EnrollmentPK implements Serializable {
  public long student;
  public long course;

  public EnrollmentPK()                 { }
  public EnrollmentPK( long s, long c ) { student = s;  course  = c; }

  public boolean equals( Object rhs ) {
    if (rhs==null || this.getClass() != rhs.getClass())
      return false;
    return (this.student == ((EnrollmentPK)rhs).student
                &&   this.course == ((EnrollmentPK)rhs).course);
  }
  public int hashCode() {
    String strResult = "" + student + course;
    return strResult.hashCode();
  }
  public String toString() {
    String className = this.getClass().getName();
    String name = className.substring( 1+className.lastIndexOf('.'),
                                       className.length() );
    return name + "(" + student + ", " + course + ")";
} }

/****************************************
Primary Key Class
  association.EnrollmentPK
Primary Key Field Name
  [empty]
ejbStore
  [empty]
ejbCreate
  INSERT INTO "EnrollmentBeanTable"  ( "course" , "student" )
  VALUES (  ?  ,  ?  )
ejbRemove
  DELETE FROM "EnrollmentBeanTable"
  WHERE "course" = ?  AND "student" = ?
findByPrimaryKey
  SELECT "course" , "student" FROM "EnrollmentBeanTable"
  WHERE "course" = ?  AND "student" = ? 
findByStudent( long parameter1 )
  SELECT "course" , "student" FROM "EnrollmentBeanTable"
  WHERE "student" = ?1
findByCourse( long parameter1 )
  SELECT "course" , "student" FROM "EnrollmentBeanTable"
  WHERE "course" = ?1
ejbLoad
  SELECT  *  FROM "EnrollmentBeanTable"
  WHERE "course" = ?  AND "student" = ? 
table create
  CREATE TABLE "EnrollmentBeanTable"  ("course" LONGINT NOT NULL ,
  "student" LONGINT NOT NULL, CONSTRAINT "pk_EnrollmentBeanTable"
  PRIMARY KEY ("course" , "student") )
table delete
  DROP TABLE "EnrollmentBeanTable" 
****************************************/



public class TestClient {
  public static void main( String[] args ) {
    try {
      InitialContext ic = new InitialContext();     Object obj;
      obj = ic.lookup( StudentHome.JNDI_NAME );
      StudentHome studHome = (StudentHome)
                       PortableRemoteObject.narrow( obj, StudentHome.class );
      obj = ic.lookup( CourseHome.JNDI_NAME );
      CourseHome courHome = (CourseHome)
                       PortableRemoteObject.narrow( obj, CourseHome.class );
      obj = ic.lookup( EnrollmentHome.JNDI_NAME );
      EnrollmentHome enroHome = (EnrollmentHome)
                       PortableRemoteObject.narrow( obj, EnrollmentHome.class );
      Student s1 = studHome.create( "Tom" );
      Student s2 = studHome.create( "Dick" );
      Student s3 = studHome.create( "Harry" );
      Course  c1 = courHome.create( "J2EE" );
      Course  c2 = courHome.create( "ECOD" );
      Course  c3 = courHome.create( "JWEB" );
      enroHome.create( s1, c1 );       enroHome.create( s2, c1 );
      enroHome.create( s1, c2 );       enroHome.create( s2, c2 );
      enroHome.create( s1, c3 );       enroHome.create( s3, c1 );

      Enrollment enro = enroHome.findByPrimaryKey( new EnrollmentPK(2,2) );
      System.out.println( "find by primary key 2,2 - " + enro.getSummary() );

      Collection coll = enroHome.findByStudent( s1.getId() );
      for (Iterator it = coll.iterator(); it.hasNext(); ) {
        enro = (Enrollment) PortableRemoteObject.narrow( it.next(), Enrollment.class );
        System.out.println( "find by student 1 - " + enro.getSummary() );
      }
      coll = enroHome.findByCourse( c2.getId() );
      for (Iterator it = coll.iterator(); it.hasNext(); ) {
        enro = (Enrollment) PortableRemoteObject.narrow( it.next(), Enrollment.class );
        System.out.println( "find by course 2 - " + enro.getSummary() );
      }
    } catch (CreateException ex) { ex.printStackTrace();
    } catch (FinderException ex) { ex.printStackTrace();
    } catch (RemoteException ex) { ex.printStackTrace();
} } }

// D:> java association.TestClient
// find by primary key 2,2 - student-2, course-2
// find by student 1 - student-1, course-1
// find by student 1 - student-1, course-2
// find by student 1 - student-1, course-3
// find by course 2 - student-1, course-2
// find by course 2 - student-2, course-2