Newbie : How to communicate between Session EJB and Entity EJB


EJB programming & troubleshooting: Newbie : How to communicate between Session EJB and Entity EJB

  1. Hello All,

    In the past few days I have been struggling to understand how exactly Session EJB's (remote interfaces) and Entity EJB's (local interfaces) communicate. I have read up on JNDI which seems to be the way to go, but I there is still a few things unclear to me.

    I have created a simple session bean Catalogue. Now I have created a method in Catalogue to lookup my Entity Beans so that I can use them for my business logic. The following method provides, what I think, could be used to obtain a reference to a Entity Bean. I am using CMP, so if that makes any difference in terms of lookup code I would be glad to hear about it.

    // Lookup my entity beans
    public void lookupEntityBeans () throws NamingException {
       Context ctx = new InitialContext();
       Account accountEntity = (Account)ctx.lookup("Account");
       Part partEntity = (Part)ctx.lookup("Part");

    Now from my Catalogue Session Bean, I wish to a get the discount which is associated with a particular customer, which is stored in the Account Entity Bean. Upon retrieving that value I would store it in a local variable. Then get the price of a particular part from a Part Entity Bean and store it in a local variable as well. Then perform my business logic using the values I have extracted from the 2 entity beans.

    // Business Method
    public double computeNewPartPrice (double oldPartPrice) {
       // getPartPrice is an abstract method defined in
       // the local interface
       oldPartPrice = partEntity.getPartPrice();
       tempAccountDiscount = accountEntity.getAccountDiscount();
       newPartPrice = oldPartPrice - tempAccountDiscount;

    Question 1

    Basically, is the above code listings enough to get the necessary information that I need from the entity beans?
    Do I need to declare any JNDI context code in my entity beans or is that done by the container?

    Question 2

    If I want to find say a customer discount value, which is located in the Account Entity Bean, should I use the findByCustomerID(String custID) method OR is it taken care of for me as suggested by Mastering EJB?

    Question 3

    If I use CMP Entity Beans, do I still need a AccountPK class OR is that taken care of by the container as well?

  2. Have a look at this tutorial and should explain yr queries.