RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Moving On: Migrating from EJB 2.1 to EJB 3.0 : Page 3

Find out how much you can simplify your EJB code by working through these examples, which show the process for migrating EJBs to version 3.0.

Migrating Entity Beans
This section shows how to migrate an EJB 2.1 entity bean to the EJB 3.0 specification. An EJB 2.1 entity bean implements the EntityBean interface and consists of getter and setter CMP field methods, getter and setter CMR field methods, callback methods, and ejbCreate/ejbPostCreate methods. The example entity bean, BookCatalogBean.java (see Listing 1), consists of the CMP fields title, author, and publisher, and the CMR field editions.

The ejb-jar.xml deployment descriptor for this EJB 2.1 entity bean (see Listing 2), specifies the EJB classes and interfaces, CMP fields, EJB QL queries, and CMR relationships. The BookCatalogBean entity bean defines a finder method, findByTitle(), and a CMR field, editions.

In contrast, the EJB 3.0 entity bean class that corresponds to the EJB 2.1 bean class is a Plain Old Java Object (POJO), and it's far simpler (see Listing 3). The EJB 3.0 version of this bean class uses the metadata annotation @Entity. The finder methods, specified in the EJB 2.1 deployment descriptor ejb-jar.xml file with elements, are specified in the EJB 3.0 bean class with the @NamedQueries and @NamedQuery annotations. The CMR relationships, specified in the ejb-jar.xml file with elements, are specified in the EJB 3.0 bean class with metadata annotations. The primary key field is specified with the annotation @Id. Some of the EJB 3.0 metadata annotations are discussed in Table 1.

Table 1. EJB 3.0 Metadata Annotations: The table shows common EJB 3.0 metadata annotations, a brief description, and when appropriate, the elements affected by the annotation.
Annotation Description Annotation Elements
@Entity Annotates an entity bean class.  
@Table Annotates the entity bean table. If @Table is not specified, the table name is same as the EJB name. name, schema
@Id Annotates a primary key property or field.  
@Transient Annotates a non persistent property or field.  
@Column Annotates a mapped column for a persistent entity bean property. name, primaryKey, nullable, length.Default column name is the property or field name.
@NamedQueries Annotates a group of named queries.  
@NamedQuery Annotates a named query or a query associated with a finder method name, queryString
@OneToMany Annotates a one-to-many association. Cascade
@OneToOne Annotates a one-to-one association. Cascade
@ManyToMany Annotates a many-to-many association. Cascade
@ManyToOne Annotates a many-to-one association. Cascade

You'll find the EJB 3.0 annotation type corresponding to the finder method findByTitle() in the EJB 2.1 bean class, the EJB 3.0 version uses the @NamedQuery annotation. Corresponding to the CMR relationship BookCatalog-Editions in the EJB 2.1 entity bean, the EJB 3.0 entity bean uses a @OneToMany annotation. The @Id annotation annotates the identifier property title. The @Column annotation specifies the database column that corresponds to the identifier property title. If a persistent entity bean property is not annotated with @Column annotation, the EJB server assumes the column name is same as the entity bean property name. Transient entity bean properties are annotated with the @Transient annotation.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date