Antipattern: Square Bean in a Round Hole
In AntiPatterns , the authors presented an antipattern called the Golden Ham-
mer. This is what they had to say about the anecdotal evidence:
“I have a hammer and everything else is a nail.” “Our database is our archi-
tecture.” “Maybe we shouldn't have used Excel macros for this job after all.”
For EJB s, the Golden Hammer is usually the entity bean with container-
managed persistence. This is also the EJB classification with the most over-
head. If every object in our model is a container-managed entity bean, we
might want to hunker down behind the desk to prepare for the deluge of cus-
tomer complaints about performance. We should instead limit the use of
entity beans to problems that need them. The characteristics of this EJB classi-
Transactional. The application should have transactional characteristics.
Entity beans will maintain their value after a crash, and they will also
maintain transactional integrity. Such functionality is valuable, but it
comes at a cost.
Persistent. This is the most obvious of the characteristics. None of the
examples in this chapter require you to write a single line of database
code. The container will do all of that for you.
Shared. We can share the same model across many different users, which
is one of the founding concepts of EJB s.
These characteristics are incredibly valuable. Distributed, transactional,
shared, and persistent architectures that used to take years can be developed in
mere months. The danger is that entity beans become our Golden Hammer.
In the coming section, we'll break form slightly. We'll show a series of
mini-antipatterns followed by solutions. In the first, we will show that we can
also go overboard with our entity beans.
Mini-antipattern: Bean-Managed Joins
My first question when I saw that I could create entity beans with bean-man-
aged persistence was, “Why would I?” Isn't that like ordering a hamburger
without the meat? “Yes, I'm ready to order. I'll have a persistence framework,
hold the persistence.” Like any framework, Java's bean-managed persistence
handles some problems better than others. For example, join algorithms are
handled more quickly in the database engine.
The first mini-antipattern involves processing a join, but outside the data-
base engine. A database join is the process of combining two sets of database