Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Container-managed relations for the 21st century

EJB 2.0's new relationship facilities assist developers in rapid deployment

  • Print
  • Feedback

Java developers often create complex relationships when they program applications to solve real-world business problems. Therefore, it was no surprise that when programmers began developing enterprise applications using Enterprise JavaBeans (EJB), they continued to describe their business models with complex relationships. In the original specification, EJBs, however, made such complex relationships more difficult because developers had little control over when an EJB was activated or passivated. In response, developers often wrote complex logic to ensure object persistence within these relationships.

Luckily, the EJB specification developers, recognizing the problem, added container-managed relationships (CMR) in EJB 2.0.

What are these relationships? How do you describe them? How do you encode them in your Java classes? In this article I answer those questions and others.

Note: I assume you understand Java and EJB. Although I give a CMP 2 (container-managed persistence) overview, you won't find an exhaustive CMP tutorial here. See Resources for more articles on EJB and CMP.

Also note: You can download this article's code examples from Resources.

CMP 2 overview

Before you can understand EJB relationships, you must first understand how EJBs work. How are their persistent fields defined? How do you define the relationships? What are the relationships' return objects?

The EJB 1.0 and 1.1 specifications identified container-managed fields by placing attributes in the bean, then described the fields in the deployment descriptor. Although other entity beans could reside within the parent entity bean, the container did not automatically handle their persistence or loading. The bean developer, therefore, had to write additional code, normally in the ejbCreate(), ejbActivate(), and ejbPassivate() methods to handle these additional entity beans. The developer, as his only alternative, could define entity beans using bean-managed persistence.

  • Print
  • Feedback

Resources