Java
  Home arrow Java arrow Developing Your First Beans
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Developing Your First Beans
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2006-11-09

    Table of Contents:
  • Developing Your First Beans
  • Developing a Session Bean
  • Creating a CABIN Table in the Database
  • Creating a Client Application
  • Creating a new Cabin entity

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Developing Your First Beans


    (Page 1 of 5 )

    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).

    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
      @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 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>
        <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.

    More Java Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Enterprise JavaBeans 3.0 Fifth Edition,"...
     

    Buy this book now. This article 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). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT