how to write primarykeyclass in this situation

Discussions

EJB design: how to write primarykeyclass in this situation

  1. hello friend,

    i am k.ramakrishna. i have taken your mailid from serverside and sending a query which i could not solve

    i our design we have two tables :
    category(category_id varchar(8) primary key)
    subcategory(category_id varchar(8) ,subcategory_id varchar(8) ,
    primary key(category_id,subcategory_id),
    foreign key(category_id) referencing category(category_id) )

    now through a single entity bean i have to insert a record in two tables. i have taken all the parameters i.e both tables in create and inserting records in table category in create and inserting in subcategory in postcreate method. i am confused in writing the primary key class . can you suggest me how to proceed in this situation. is it neccessary that we have to take two beans for this situation and insert independently.

    with regards
    k.ramakrishna
    (kalavendi at yahoo dot com)
  2. public class CatIDKey implements java.io.Serializable{
       
       public int CompID;

    /**
    * Default Contructor
    */
       public CatIDKey(){
          super();
       }

    /**
    * Initialize a key from the passed values
    * @param argPrimaryKey int
    */
       public CatIDKey(int argPrimaryKey){
          CompID = argPrimaryKey;
       }

    /**
    * Initialize a key from the passed values
    * @param argPrimaryKey Integer
    */
    public CatIDKey(Integer argPrimaryKey){
    CompID = Integer.parseInt(argPrimaryKey.toString());
    }

    /**
    * equals method
    * @return boolean
    * @param o java.lang.Object
    */
    public boolean equals(Object o){
          if (o instanceof CompanyIDKey){
             CompanyIDKey otherKey = (CompanyIDKey) o;
             return (CompID == otherKey.CompID);
          }
          else
             return false;
       }

    /**
    * hashCode method
    * @return int
    */
       public int hashCode(){
          return CompID;
       }

    }

    This will be your primary key

    Now in Entity bean you have to write in findbyPrimaryKey:

    1)First fetch the connection
    2)Create a prepared statement say "SELECT * FROM CAT WHERE CATEGORY_ID = ?"
    3)Now use something like "ps.setInt(1,pcPK.CompID); "
    4)Now Execute this thing.
    5)Check if the primary key exist.If no throw an Exception.
    6)Now use the same result set which was use to run to reteive the result when u checked if the category exist.
    7)Now prepare another Statement using the different SQL like
    "Select subcategory_ID from <second_table> where CompID = ?"
    8)Now replace this value from the same company id to fetch the Primary key of the other table.
    9) now fill the Holder of your bean with the data. If you think the second query returns many rows ..Maybe you can opt for an array of a second holder inside the first holder
  3. public class CatIDKey implements java.io.Serializable{
       
       public int CompID;

    /**
    * Default Contructor
    */
       public CatIDKey(){
          super();
       }

    /**
    * Initialize a key from the passed values
    * @param argPrimaryKey int
    */
       public CatIDKey(int argPrimaryKey){
          CompID = argPrimaryKey;
       }

    /**
    * Initialize a key from the passed values
    * @param argPrimaryKey Integer
    */
    public CatIDKey(Integer argPrimaryKey){
    CompID = Integer.parseInt(argPrimaryKey.toString());
    }

    /**
    * equals method
    * @return boolean
    * @param o java.lang.Object
    */
    public boolean equals(Object o){
          if (o instanceof CatIDKey ){
             CatIDKey otherKey = (CatIDKey ) o;
             return (CompID == otherKey.CompID);
          }
          else
             return false;
       }

    /**
    * hashCode method
    * @return int
    */
       public int hashCode(){
          return CompID;
       }

    }

    This will be your primary key

    Now in Entity bean you have to write in findbyPrimaryKey:

    1)First fetch the connection
    2)Create a prepared statement say "SELECT * FROM CAT WHERE CATEGORY_ID = ?"
    3)Now use something like "ps.setInt(1,pcPK.CompID); "
    4)Now Execute this thing.
    5)Check if the primary key exist.If no throw an Exception.
    6)Now use the same result set which was use to run to reteive the result when u checked if the category exist.
    7)Now prepare another Statement using the different SQL like
    "Select subcategory_ID from <second_table> where CompID = ?"
    8)Now replace this value from the same company id to fetch the Primary key of the other table.
    9) now fill the Holder of your bean with the data. If you think the second query returns many rows ..Maybe you can opt for an array of a second holder inside the first holder