Home arrow Java arrow Developing Your First Beans

Developing Your First Beans

If you're looking for a good place to start learning how to create a Java Bean, look no further. This article explains how to create an entity bean. It is taken from chapter 4 of the book Enterprise JavaBeans 3.0 Fifth Edition, written by Richard Monson-Haefel and Bill Burke (O'Reilly, 2006; ISBN: 059600978X).

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 14
November 09, 2006
  1. · Developing Your First Beans
  2. · Developing a Session Bean
  3. · Creating a CABIN Table in the Database
  4. · Creating a Client Application
  5. · Creating a new Cabin entity

print this article

Developing Your First Beans
(Page 1 of 5 )

The primary goal of the EJB 3.0 and Java Persistence specifications was to make it as easy as possible to write and deploy an EJB-based application. Creating an application is as easy as compiling your code, jarring up your classes, and running your application server. This chapter gives an introduction to writing your first entity and session bean. Youll find that getting up and running is fairly simple.

Developing an Entity Bean

Lets start by examining how to create an entity bean. Well 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 Titans 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 meta-data. 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.*;

  @Entit y
  public class Cabin implements java.io.Serializable{

      private int id;
      private String name;
      private int deckLevel;
      private int shipId;
      private int bedCount;

      public int getId() { return id; }
      public void setId(int pk) { id = pk; }

      public String getName() { return name; }
      public void setName(String str) {name = str; }

      public int getDeckLevel() { return deckLevel; }
      public void setDeckLevel(int level) { deckLevel = level; }

      public int getShipId() { return shipId; }
      public void setShipId(int sid) { shipId = sid; }

      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 data base 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 beans 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 theyre 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 4-1. 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 IDEs compiler, should be written to the domain directory, the same directory as the Cabin.java file. The rest of the Cabin beans classes will be placed in this same directory. We are done defining the Cabin entity.

Figure 4-1.  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-unit name="titan">

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.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials