This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I don't think there is a compulsion or binding to use BMP in any scenerio. But following are the advantages of BMP over CMP. 1. It is easier to debug. 2. It gives tremendous flexibility on how you want to map your objects to the database. Regards, Ashoke
One of my favorite comprehensive J2EE references is "Professional Java Server Programming, J2EE 1.3 Edition" by Allamaraju et al. (WROX). Their recommendation for CMP v. BMP is to stick with CMP unless you really need BMP: "...The basic problem is that, although every EJB 2.0-compliant container must provide support for container-managed persistence, the specification does not indicate *how* this support must be provided. In fact, a container need not necessarily provide any CMP support for mapping your entity bean's state to columns in a database. It could use Java serialization to write the whole bean to one column and still be compliant. This would rarely be adequate for a project, and might necessitate the use of bean-managed persistence if your EJB container used serialization. "On the other hand, this extreme case doesn't exist in real life. Every single application server with EJB support of which I am aware, including the open source ones, has some support for mapping your objects to the fields in a relational database. "The trick is determining whether your EJB container has the level of support for container-managed persistence that you require. If it does, take advantage of this support to free your business-logic programmers from writing persistence logic. The boost in productivity is potentially very large, and depending on the EJB container, the boost in application performance may be large, too." It goes on to say that you will probably need to use BMP is your target data store is an ERP system or some other existing application.
Hi Rajesh, The difference is that CMP beans rely on the container for the persistence of a bean's data and is hence easier to develop, whereas BMP beans rely on the bean to persist its own data. But to decide on when to go for BMP is more of a design decision that weighs on performance, flexibility, database independence, application server independence, maintainability, and convenience. 1. Many times Performance is one of the only bottle neck so if your beans can be deployed in any application server, and you know what you are doing with JDBC and EJB (a very big concern!), BMP should always win out over CMP in performance. 2. Bean-managed beans also offer greater flexibility with respect to the type of data they are representing.Sometimes, the nature of your database schema does not mix well with the object-oriented representation that you are trying to develop, leaving you with no choice other than to use BMP. 3. BMP beans, to some degree, are more application server-independent; they are more dependent on the database schema. Once the database schema is in place, however, BMP beans are easier to move around to different application servers. 4. Bean-managed beans have their downside too: maintainability and convenience. BMP beans are tied very closely to a database schema (the code to access specific tables and column names is hard-coded in the bean itself.) CMP beans can be configured at deployment time; a bean can be mapped to a specific table and its fields to that table's columns. A change in the database schema does not necessarily equate to a change in the bean; a smart object-mapping tool can hide some of the changes, but a major overhaul will affect the bean code, regardless. Regards Malay