Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Implementing an EJB 3.0 Entity

EJB 3.0 greatly simplifies the development of EJBs, removing many complex development tasks. For example:

- The bean class can be a plain old Java object (POJO); it does not need to implement `javax.ejb.EntityBean`.

- Home (`javax.ejb.EJBHome`) and component (`javax.ejb.EJBObject`) interfaces are not required.

  If you choose to use a remote component interface, you can use a plain old Java interface (POJI); it does not need to extend `javax.ejb.EJBObject`.

- Annotations are used for many features, including container-managed relationships (object-relational mapping).

- An `EntityContext` is not required: you can simply use `this` to resolve an entity to itself.

For more information, see "What is an EJB 3.0 Entity?".

Note: You can download an EJB 3.0 CMP entity code example from: `http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3/howtoejb30entity/doc/how-to-ejb30-entity-ejb.html`.

To implement an EJB 3.0 entity:

1. Create the entity bean class.

   You can create a plain old Java object (POJO) and define it as a CMP entity bean with the `@Entity` annotation.

   All data members are by default considered container-managed persistence fields unless annotated with `@Transient`.

2. Define how OC4J persists your entity bean class to a database using the `@Table` and `@Column` annotations.

   If you do not have an existing database schema, you can delegate table and column definition to OC4J by omitting these annotations: at deployment time, OC4J will create default table and column names based on class and data member names.

   For more information, see "Configuring Table and Column Information".

3. Define one data member as the primary key field with the `@Id` annotation.

   You can annotate the data member itself or its getter method. For more information, see "Configuring an EJB 3.0 Entity Primary Key".

4. Define container-managed relationships using the appropriate object-relational mapping annotations, such as `@OneToOne`.

   For more information, see "Configuring an EJB 3.0 Entity Container-Managed Relationship Field".

5. Optionally, define finders and queries using the `@NamedQuery` annotation.

   At runtime, you can use the predefined finders (see "Predefined TopLink Finders") and default finders (see "Default TopLink Finders") that the TopLink persistence manager provides.

   For more information, see "Using EJB 3.0 Query API".

6. Optionally, define lifecycle callback methods using the appropriate annotations.

   You do not need to define lifecycle methods: OC4J provides an implementation for all such methods. Define a method of your entity bean class as a lifecycle callback method only if you want to take some action of your own at a particular point in the entity bean's lifecycle.

   For more information, see "Configuring a Lifecycle Callback Method for an EJB 3.0 Entity".

7. Complete the configuration of your entity bean (see "Using EJB 3.0 Persistence API").