Object identity: the primary key

Every entity MUST have a unique identity. The Container will never let you get away with having two or more entities with the same primary key. And as of EJB 2.0, coming up with that primary key is still your job. The Container won’t do it (at least not according to the specification).

image with no caption

With CMP, you’re still responsible for the primary key. That doesn’t mean you won’t use a primary key service of some kind. Maybe you have a service that automatically allocates a big block of primary keys from the database, and then hands them out as needed. Or you might have some type of unique identifier algorithm that makes unique keys for you. Or maybe you’re using the customer’s social security number or account number or... that’s up to you to decide.

And the way you tell the Container what value the about-to-be-created entity should have is through the value of one or more of your persistent fields.

If you have just one field as your primary key, and it maps directly to a column in the database, you’re set. But if you need more than one value to uniquely identify an entity (like, maybe it takes a combination of name and date), you can use a compound key that uses two or more of your container-managed persistent fields.

Rules for ejbCreate()

  • You must implement the ejbCreate<method> to match each create<method> in the home interface.

  • The method name must begin with the ...

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.