Developing an Entity Bean






Developing an Entity Bean

Let's start by examining how to create an entity bean. We'll implement the Cabin entity that is part of the Titan Cruises Java EE application. The Cabin entity encapsulates the data and behavior associated with a cruise ship cabin in Titan's business domain. Although you can interact with entity beans outside of an application server, we will later create a TravelAgent session bean to serve as a data access interface for creating and locating cabins.

Cabin: The Bean Class

When developing an entity bean, all we need to define is the bean class. Entities in Java Persistence are plain Java objects that are annotated with O/R mapping metadata. We started to define the Cabin entity in Chapter 2; here, we add two new methods for setting and getting the ship ID and the bed count. The ship ID identifies the ship to which the cabin belongs, and the bed count tells how many people the cabin can accommodate:

package com.titan.domain

import javax.persistence.*;

@Entity
@Table(name="CABIN")
public class Cabin implements java.io.Serializable{
    private int id;
    private String name;
    private int deckLevel;
    private int shipId;
    private int bedCount;

    @Id
    @Column(name="ID")
    public int getId( ) { return id; }
    public void setId(int pk) { id = pk; }

    @Column(name="NAME")
    public String getName( ) { return name; }
    public void setName(String str) {name = str; }

    @Column(name="DECK_LEVEL")
    public int getDeckLevel( ) { return deckLevel; }
    public void setDeckLevel(int level) { deckLevel = level; }

    @Column(name="SHIP_ID")
    public int getShipId( ) { return shipId; }
    public void setShipId(int sid) { shipId = sid; }

    @Column(name="BED_COUNT")
    public int getBedCount( ) { return bedCount; }
    public void setBedCount(int bed) { bedCount = bed; }

}

The Cabin bean class is annotated with @javax.persistence.Entity and @javax.persistence.Table . The @Entity annotation tells the persistence provider that this is an entity class that is mapped to a database and that can be managed by an EntityManager service. The @Table annotation tells the EJB container to which database table the bean class should map. The bean class implements java.io.Serializable , but it is not required to. Having entity classes implement Serializable allows them to be used as the parameters and return values of the remote interface methods of a session bean. This allows you to use the same class for both persistence and data transfer.

Cabin also defines four properties: name, deckLevel, shipId, and bedCount. Properties are attributes of an entity bean that can be accessed by public set and get methods; they can also be accessed directly through the bean's fields. In this example, we use public set and get methods. For each property, we define how it maps to the columns in the CABIN database table with the @javax.persistence.Column annotation. The getId( ) property is marked as the primary key of the Cabin entity by using the @javax.persistence.Id annotation. The primary key denotes the identity of a particular entity bean at runtime and when it is persisted in the database.

Table-mapping annotations like @Table and @Column are not required; if they're omitted, they default to the unqualified name of the class and the property name, respectively. However, primary-key identification is required.


Notice that we have made the Cabin bean class a part of a new package named com.titan .domain. Place all the classes and interfaces associated with each type of bean in a package specific to the bean. Because our beans are for use by Titan Cruises, we placed these packages in the com.titan package hierarchy. We also created directory structures that match package structures. If you are using an IDE that works directly with Java files, create a new directory called dev (for development) and create the directory structure shown in Figure. Copy the Cabin bean class into your IDE and save its definition to the domain directory. Compile the Cabin bean class to ensure that its definition is correct. The Cabin.class file, generated by the IDE's compiler, should be written to the domain directory, the same directory as the Cabin.java file. The rest of the Cabin bean's classes will be placed in this same directory. We are done defining the Cabin entity.

dev directory structure


The persistence.xml File

The Java Persistence specification requires a simple XML deployment descriptor file, persistence.xml, which configures basic things like the name of the EntityManager service that will be managing the set of entities deployed in a particular deployment package. It also defines what database the EntityManager service will be communicating with and may also specify additional, vendor-specific properties.

<persistence>
   <persistence-unit name="titan">
      <jta-data-source>java:/TitanDB</jta-data-source>
   </persistence-unit>
</persistence

The <name> element represents the set of classes that are managed by a given EntityManager. The <jta-data-source> element defines the database that will be used to persist entities in this deployment. This persistence.xml file is located in a META-INF directory. persistence.xml is discussed in detail in Chapter 5.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows