Discussions

EJB design: Using compound key as primary key in EJB

  1. Hi All,

    I am using compound key as primary key.I had writtenprimary key class.In bean for ejbCreate() method return type I have given return type as this primary key class.While compiling with ejbc I am getting the following error.

    C:\Reddy\bookvilla\jars\catent>ejbc -d . test.jar
    C:\Reddy\bookvilla\jars\catent\.\Cart1\CartBeanEOImpl.java:56: incompatible type
    s
    found : java.lang.String
    required: Cart1.CartPK
    pk = bean.book_id;
             ^
    1 error
    Exec failed .. exiting


    Here I am giving my primary key class for ur reference.
    Please help me to come out from this error.

    package Cart1;

    public class CartPK implements java.io.Serializable {
       public String book_id;
       public String merch_id;
    // public CartPK() { }


    public CartPK(String bookid, String merchid){

    this.book_id=bookid;
    this.merch_id=merchid;
    }


    public String getbookid(){
    return book_id;

    }

    public String getmerchid(){
    return merch_id;

    }
       public boolean equals(Object other) {

          if (other instanceof CartPK) {
             return (book_id.equals(((CartPK)other).book_id)
                     && merch_id.equals(((CartPK)other).merch_id));
          }

          return false;
       }

       public int hashCode() {

          return book_id.hashCode();
       }




    }



    Thanks in advance,
    Srinivasa Rao
  2. Hi
    I think u shud have sent the BEAN class where ur using the primary key class.and yes also tell whether ur using BMP or CMP,
    so do send the code for Bean class so that we can debug the problem.
  3. hi
    when u use a compund primary key you have to override the HASHcODE METHOD TOO.
    Here is sample codefor has code
    public int hashCode(){
      StringBuffer buff = new StringBuffer ();
      buff.append(strPK1);
      buff.append(strPK2);
      int hashCode =buff.tostring().hashCode();
      return hashCode;
    }
    where strPK1,strPK2 are public instance variables of your Primaey class which together form the CompoundPrimaryKey .

    Neeraj
  4. Hi Srinivas,

    Remove the primary key field name in the XML file , it will work fine.MAke sure you have entered the primary class name in XML file as your primary key class name

    Hope this helps you
  5. Hi Srinivas,

    Remove the primary key field name in the XML file , it will work fine.MAke sure you have entered the primary class name in XML file as your primary key class name

    Hope this helps you
  6. I try to figure out the compound key with CMP EJB, and I browse many articles about this. It's useful but not too details. Finally I work it. There I would like to post it about my works:

    I use Sun Deploytool in J2EE.

    OrderItemEJB:
    * Entity Bean representing a OrderItem.
     */

    import javax.ejb.EntityBean;
    import javax.ejb.EntityContext;
    import javax.ejb.CreateException;

    public class OrderItemEJB implements EntityBean {

       public OrderItemPK pKey;
       public String order_no;
       public String item_no;
       public int quantity;
       public double price;

       private EntityContext context;

       public String getOrderNumber() {
         return order_no;
       }
       public void setOrderNumber(String ordernumber) {
         this.order_no = ordernumber;
       }
       public String getItemNumber() {
         return item_no;
       }
       public void setItemNumber(String itemnumber) {
    this.item_no = itemnumber;
       }

       public int getQuantity() {
         return quantity;
       }
       public void setQuantity(int quantity) {
         this.quantity = quantity;
       }

       public double getPrice() {
         return price;
       }
       public void setPrice(double price) {
         this.price = price;
       }

       public String ejbCreate(OrderItemPK orderitemKey, int quantity, double price) throws CreateException {

         if (orderitemKey == null) {
           throw new CreateException("order & item is required.");
         }

         this.pKey = orderitemKey;
         this.order_no = pKey.getOrderID();
         this.item_no = pKey.getItemID();
         this.quantity = quantity;
         this.price = price;

         return null;
       }
       public void ejbPostCreate(OrderItemPK orderitemKey, int quantity, double price) { }

       public void setEntityContext(EntityContext context) {
         this.context = context;
       }
       public void unsetEntityContext() { }
       public void ejbActivate() { }
       public void ejbPassivate() { }
       public void ejbRemove() { }
       public void ejbLoad() { }
       public void ejbStore() { }
    }

    OrderItemHome:
    /* Specifies methods to create and find a Order.
     */

    import java.rmi.RemoteException;
    import javax.ejb.CreateException;
    import javax.ejb.FinderException;
    import javax.ejb.EJBHome;
    import java.util.Enumeration;

    public interface OrderItemHome extends EJBHome {
      OrderItemRemote create(OrderItemPK orderitemKey, int quantity, double price)
                       throws RemoteException, CreateException;
      OrderItemRemote findByPrimaryKey(OrderItemPK orderitemKey)
                       throws FinderException, RemoteException;
    }

    OrderItemRemote:
    /* Specifies business methods for Orders EJB.
     */

    import javax.ejb.EJBObject;
    import java.rmi.RemoteException;


    public interface OrderItemRemote extends EJBObject {
      public String getOrderNumber() throws RemoteException;
      public void setOrderNumber(String ordernumber) throws RemoteException;
      public String getItemNumber() throws RemoteException;
      public void setItemNumber(String itemnumber) throws RemoteException;
      public int getQuantity() throws RemoteException;
      public void setQuantity(int quantity) throws RemoteException;
      public double getPrice() throws RemoteException;
      public void setPrice(double price) throws RemoteException;
    }

    OrderItemClient:
    /* Creates five customers. Tests the finder methods.
     */

    import java.util.Enumeration;
    import javax.naming.Context;
    import javax.naming.InitialContext;

    public class OrderItemClient {
      public static void main(String[] args) {
        try {
           Context initial = new InitialContext();
           OrderItemHome home =
               (OrderItemHome)initial.lookup("OrderItemHome");

           OrderItemRemote orderitem = home.create(new OrderItemPK("1", "222222"), 4, 27.00);
           orderitem = home.create(new OrderItemPK("1", "333333"), 2, 210.50 );
           orderitem = home.create(new OrderItemPK("1", "444444"), 1, 569.00 );
           orderitem = home.create(new OrderItemPK("2", "333333"), 2, 230.95 );
           orderitem = home.create(new OrderItemPK("3", "222222"), 3, 27.00 );
           orderitem = home.create(new OrderItemPK("3", "333333"), 1, 230.95 );
           orderitem = home.create(new OrderItemPK("4", "444444"), 1, 569.00 );
           orderitem = home.create(new OrderItemPK("5", "222222"), 2, 27.00 );
           orderitem = home.create(new OrderItemPK("5", "444444"), 1, 725.00 );


           // test findByPrimaryKey
           System.out.println("test findByPrimaryKey");

           OrderItemPK pKey= new OrderItemPK("1","222222");
           OrderItemRemote orderitemKey = home.findByPrimaryKey(pKey);

           String order_no = (String) orderitemKey.getOrderNumber();
           String item_no = (String) orderitemKey.getItemNumber();
    int quantity = orderitemKey.getQuantity();
    double price = orderitemKey.getPrice();

    System.out.println(order_no + ' ' + item_no + ' ' + quantity + ' ' + price);


        }catch (Exception e) {
               e.printStackTrace();
        }
      }
    }
  7. Sorry, I forgot to post the primarykey class. Here you go:
    Welcome any suggestion to me.

    OrderItemPK:

    import java.io.Serializable;

    public class OrderItemPK implements java.io.Serializable {
       public String order_no;
       public String item_no;

       public OrderItemPK() {}

       public OrderItemPK(String ordernumber, String itemnumber) {
    this.order_no = ordernumber;
    this.item_no = itemnumber;
       }

       public String getOrderID() {
         return order_no;
       }

       public String getItemID() {
            return item_no;
       }

       public String toString() {
    return this.toString();
       }

       public int hashCode() {
    StringBuffer strBuff = new StringBuffer();
    strBuff.append(order_no);
    strBuff.append(item_no);
    String str = strBuff.toString();
    int hashCode = str.hashCode();
    return hashCode;
       }

       public boolean equals(Object other) {

          if (other instanceof OrderItemPK) {
             return (order_no.equals(((OrderItemPK) other).order_no)
                     && item_no.equals(((OrderItemPK) other).item_no));
          }

          return false;
       }
    }