when should we go either BMP or CMP

Discussions

EJB design: when should we go either BMP or CMP

  1. when should we go either BMP or CMP (6 messages)

    hello everybody,

    when should we go for CMP in comparision to the BMP?
    what are the advantages of BMP over CMP ?

    with regards
    k.ramakrishna


    Threaded Messages (6)

  2. Hi Ramakrishna,
       Pls go thru this:

    Advantages of CMP:-

    1. less coding effort
    2. the container will generate efficient data access layer for our CMP
    3. perfectly OK if we are going to have one ejb for one table in the database

    Situations where CMP can't be used:

    1. one ejb mapped to multiple tables in the database or vice-versa
    2. If we've to write some complex queries in the finder methods. Such nested queries can't be written with the help of the server's query language.

    thanks...hasan
    (ahamed at aztec dot soft dot net)

  3. when should we go either BMP or CMP[ Go to top ]

    Hi Hasan

    Advantages of CMP:-

    As you said go for CMP if we are going to have one ejb for one table in the database, but we can use CMP for achieving one to many kind of relationship using ejbPostCreate() method. The bean will be use CMP for master tables and the accessing details tables will be done in ejbPostCreate() method. It is partially CMP.

    Also we can't use CMP for following:

    1. Legacy systems.
    2. whenever we have more than one database(e.g Oracle and SQL server) to access as CMP supports only one database access at a time.

    3.
  4. when should we go either BMP or CMP[ Go to top ]

    Hi Hasan

    Advantages of CMP:-

    As you said go for CMP if we are going to have one ejb for one table in the database, but we can use CMP for achieving one to many kind of relationship using ejbPostCreate() method. The bean will be use CMP for master tables and the accessing details tables will be done in ejbPostCreate() method. It is partially CMP.

    Also we can't use CMP for following:

    1. Legacy systems.
    2. whenever we have more than one database(e.g Oracle and SQL server) to access as CMP supports only one database access at a time.

    Regards
    Sanjay
  5. when should we go either BMP or CMP[ Go to top ]

    What do you mean exactly by :

    The bean will be use CMP for master tables and the accessing details tables will be done in ejbPostCreate() method.

    Can you go into an example ?

  6. when should we go either BMP or CMP[ Go to top ]

    yes.

    Here find the sample code for implementing this.

     public String strCode;
     public String strName;
    Connection dbConnection=null;

    public String ejbCreate(String strCode, String strName, Hashtable collectionOfAddress) throws CreateException{

    this.strCode = strCode;
    this.strName = strName;

    }
    public String ejbPostCreate(String strCode, String strName, Hashtable collectionOfAddress) throws CreateException{

    try{
    dbConnection = getDBConnection();// this method returns connection object
    Statement stmt = dbConnection.createStatement();
    Enumeration enum = collectionOfAddress.elements();
    while(enum.hasMoreElements()){
    AddressModel addMod = (AddressModel)enum.nextElement();
    String queryStrAddress = "INSERT INTO SalesAddress" +
    "(addressCode, addressType, addressLine1, addressLine2, addressLine3,"
    + " VALUES ("
    + "'" + addMod.strAddressCode.trim() + "',"
    + "'" + addMod.strAddressType.trim() + "',"
    + "'" + addMod.strAddressLine1.trim() + "',"
    + "'" + addMod.strAddressLine2.trim() + "',"
    + "'" + addMod.strAddressLine3.trim() + "',"
    + ")";
    try{
    int resultCountAddress = stmt.executeUpdate(queryStrAddress);
    if ( resultCountAddress != 1 )
    throw new SQLException("ERROR INSERT !! resultCount = " +
    resultCountAddress);
    }catch(SQLException e){
    System.out.println("SQLException");
    }
    }//end of while loop for Address
    }catch (java.sql.SQLException se) {
    System.out.println("SQLException");
    }finally {
    try {
    dbConnection.close();
    stmt.close();
    }catch (SQLException se) {
    System.out.println("SQLException");
    }//end of catch()
    }//end of finally()
    }//end of ejbPostCreate()
  7. when should we go either BMP or CMP[ Go to top ]

    Hi Raju,

    Here find the sample code for implementing this.
    if still you are not clear write me on sanjay_rathod at hotmail dot com
    cheers!!!
    Sanjay
    Sample Code :-

     public String strCode;
     public String strName;
    Connection dbConnection=null;

    public String ejbCreate(String strCode, String strName, Hashtable collectionOfAddress) throws CreateException{

    this.strCode = strCode;
    this.strName = strName;

    }
    public String ejbPostCreate(String strCode, String strName, Hashtable collectionOfAddress) throws CreateException{

    try{
    dbConnection = getDBConnection();// this method
                                   returns connection object
    Statement stmt = dbConnection.createStatement();
    Enumeration enum = collectionOfAddress.elements();
    while(enum.hasMoreElements()){
         AddressModel addMod = (AddressModel)enum.nextElement();
         String queryStrAddress = "INSERT INTO SalesAddress" +
    (addressCode, addressType, addressLine1,
             addressLine2, addressLine3,"
    + " VALUES ("
            + "'" + addMod.strAddressCode.trim() + "',"
    + "'" + addMod.strAddressType.trim() + "',"
    + "'" + addMod.strAddressLine1.trim() + "',"
    + "'" + addMod.strAddressLine2.trim() + "',"
    + "'" + addMod.strAddressLine3.trim() + "',"
    + ")";
    try{
    int resultCountAddress = stmt.executeUpdate(queryStrAddress);
    if ( resultCountAddress != 1 )
    throw new SQLException("ERROR INSERT !!
                  resultCount = " + resultCountAddress);
            }catch(SQLException e){
    System.out.println("SQLException");
    }
    }//end of while loop for Address
    }catch (java.sql.SQLException se) {
    System.out.println("SQLException");
    }finally {
    try {
    dbConnection.close();
    stmt.close();
    }catch (SQLException se) {
    System.out.println("SQLException");
    }//end of catch()
    }//end of finally()
    }//end of ejbPostCreate()