Defining virtual fields for persistent data fields and relationship fields

In the previous chapter we looked at defining container-managed fields—you put in a pair of abstract getters and setters. We said a container-managed field exists simply because you have a getter and setter for it. It works the same way with container-managed relationship (CMR) fields. You define a pair of abstract getters and setters, but rather than setting and returning a value that maps to a column in a table, you set and return a reference to another entity bean, or a Collection that will hold references to the entity bean. The restrictions for CMR fields are that they can refer only to the local component interface of the entity bean, and that if the method returns a Collection, the declared type can be only Collection or Set (not Map, List, etc.)

The terminology is a little confusing because both CMP and CMR fields are container-managed persistent fields. They might have called it CMPCD (container-managed persistent column data) fields and CMPR (container-managed persistent relationship) fields. Of course, persistence isn’t restricted to just relational database, so we’re using the term “column” a little loosely. But in EJB 1.1, all container-managed persistent fields were CMP fields, because there was no concept of persistent relationships in EJB 1.1 CMP.

The only difference between a CMP field and a CMR field is the TYPE.

A CMR field is always another entity bean’s local interface type, or a Collection ...

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.