Using BMP involving multiple classes and tables


EJB design: Using BMP involving multiple classes and tables

  1. Using BMP involving multiple classes and tables (8 messages)

    I am developing an entity bean which has an object as an attribute(perssistent) which has relation to multiple classes(aggregation) ,hence also multiple tables involved.
    I have kept the respective insert(create) methods in the repective classes and called these methods in the ejbCreate
    of the entity bean.Is this pattern ok..?I feel it is insecure to have the insert(create) methods in the respective classes as client(servlets) have access to these methods of the related classes.
    Please give ur comments on this pattern ASAP
  2. Hi Sourav,
       Can u explain ur design with some simple code illustrations?.

    (ahamed at aztec dot soft dot net)
  3. Here is some code:
    public class UserEJB implements EntityBean
       private Address add;
       public UserPK ejbCreate(Address add)
          java.sql.Connection con= getConnection();
          this.add.create(String pk,java.sql.Connection con);


    public class Address implements Serializable
        public void create(String pk,java.sql.Connection con)
          //Data is inserted into Database
  4. Hi Sourav,
       I assume that User and Address are two different tables in the database and u've created CMP entity beans for both of them. (Hope my assumption is right).

       There are two designs:

    First Design - Make use of a Manager Session Bean:-

    a.From ur client side u've wrapped the information like this,
         User => UserInfo (normal java bean used in JSP)
         Address => AddressInfo

    b. From ur jsp/servlet call a method on the manager session bean passing these two objects,

         UserInfo ui = new UserInfo(..., ..., ...);
         AddressInfo ai = new AddressInfo(..., ..., ...);
         sb.createUser(ui, ai);

    c. Inside the session bean the above method is implemented like this,

         public void createUser(UserInfo ui, AddressInfo ai) {
             // call create on EJB home objects of
             // User and Address beans respectively
             User user = userHome.create(ui);
             Address address = addressHome.create(ai);

    d. U've to define and implement the create method for the above signature in the two beans, like...

        UserHome Interface -
            User create(UserInfo ui)
                 throws CreateException, RemoteException;

        UserBean bean class -
            public UserPK ejbCreate(UserInfo ui) {
       = ui.getName();
       = ui.getEmail();
                return null; // incase of CMP

    Second Design:-

    In the ejbPostCreate() method of the first bean u call the create method of the second bean.

    (CompleteInfo contains both the basic information and an object for AddressInfo)

        public UserPK ejbCreate(CompleteInfo ci) {
   = ci.getName();
   = ci.getEmail();
            return null;

        public UserPK ejbPostCreate(CompleteInfo ci) {
            // acquire the home of the second bean
            AddressInfo ai = ci.getAddressInfo();
            Context ctx = new InitialContext();
            AddressHome home = (AddressHome)ctx.lookup(
            Address address = home.create(ai);
    Note: Hope u know how to make use of <ejb-ref> inorder to call other bean from a bean.

    Mail me once u r done with any one of the above designs.

    (ahamed at aztec dot soft dot net)
  5. Ur assumption is wrong.
    For me User and Address are two simple java classes.
    What u have assumed and suggested is perfectly fine but please comment on this pattern where they are just simple java classes having create methods in them which are called from ejbCreate of the entity beans.
    Waiting for ur reply
  6. Hi Sourav,
       while ur bean itself can act as the data access layer for ur system, why do u want to put one more layer in between ur bean and the DB?. Your pattern looks like this,

         Bean ==> Java Class ==> JDBC

    This is NOT an acceptable pattern and nowhere i've come across such a design. If ur going to have ur own java classes then there is no need for the corresponding entity bean. So write the code in the ejbCreate method of the bean itself, instead of calling a method on another class. Please note that method calls are very costly and should not be used un-necessarily unless and until there is some valid reason.

    (ahamed at aztec dot soft dot net)

  7. Some points regarding the design:
    1. We use these java classes to transfer data from Servlets to EJB and vice versa and also for JDBC calls.
    2.We didn't want each java class to be CMP Bean in order to reduce Bean to Bean call.(Surelly function call is more efficient than bean to bean call)
    3.We wanted to reuse the create methods as required by our Object Model.

    Yes the pattern may not be there But I wanted to know whether the pattern has any problems from Security point of view.
    Waiting for ur reply
  8. HiSourav,

    I did not understand why your design had a non component flavor in a component model. When you want to design a component model for accessing data, then I did not understand the purpose of a normal class inbetween. This model is not a problem in terms of security, but it nullifies the bean functionality.......

    And also please clarify what kind of JDBC calls you are making from the class...

  9. hi friends,
     nice to c ur discussion.I feel,the design suggested by Hasan is better,especially if the application is under high webtraffic.The container can then manage to effectively use the DB resources.Moreover future maintenance would me tough
    without the proper component d/n.