5.1 Which CMP Model to Use?
While
container-managed persistence sounds great, the initial EJB
implementations of CMP through EJB 1.1 have had some serious
drawbacks:
Most EJB servers will provide automated persistence only against
JDBC-supported database engines. If you are using some other data
storage product like an object-oriented database or a specialized
digital asset management data store, you will have to do the work
yourself. Container-managed persistence demands that your persistent attributes
have public visibility. Public attributes violate a key element of
good OO design: encapsulation. Consequently, container-managed beans
make it very easy for people to make poor design choices that rely on
the public nature of those attributes. Container-managed persistence makes it difficult to tweak your data
model for maximum performance. For example, if you want to perform
lazy-loading of certain attributes, you must use bean-managed
persistence. Container-managed persistence is incapable of supporting the complex
class relationships common to enterprise systems. For example,
container-managed persistence cannot support some one-to-many or
many-to-many relationships. If your analysis models require those
kinds of relationships, your architecture must use an alternative
persistence model.
|
Lazy-loading
is a technique through which
certain attributes in an object are not restored from the database
immediately but are instead restored in a background thread or when
the system asks for that data. A Country object,
for example, may be associated with quite a few cities—likely
more cities than you want to load into memory when most clients are
looking only for the name and ISO code for the country. Using
lazy-loading, you can put off loading all associated cities until a
later time.
|
|
In spite of its drawbacks, EJB 1.x CMP does have some redeeming
qualities. Specifically, it is a great tool if you are writing a
simple application but you do not want to worry about the database at
all. By
simple
application, I mean an application that has no complex
dependencies among entity beans and whose attributes are primitives
or serializable objects. The minute the slightest complexity enters
the picture or you need to control the data model, EJB 1.x CMP falls
apart.
|
BEST PRACTICE: Use CMP—especially EJB 2.0 CMP—whenever possible, but be prepared to use BMP if EJB 2.0 CMP is not an option.
|
|
|