Java
  Home arrow Java arrow Page 5 - 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 
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 - Creating a new Cabin entity


    (Page 5 of 5 )

    In this example, you finally see how Cabin entities are created. Java's new() operator is used to allocate an instance of the Cabin bean class. Nothing magical is happening. The client initializes the properties locally on the Cabin bean instance. The id, name, deckLevel, shipId,  and bedCount of the Cabin entity are set:

      Cabin cabin_1 = new Cabin();
      Cabin_1.setId(1);
      cabin_1.setName("Master Suite");
      cabin_1.setDeckLevel(1);
      cabin_1.setShipId(1);
      cabin_1.setBedCount(3);

    The Cabin entity does not get inserted into the database when you allocate it on the client. The instance must be passed to the TravelAgentEJB where it will be created in the database when the EntityManager.persist() method is called.

    Figure4-3 shows how the relational database table we created should look after this code has been executed. It should contain one record.


    Figure 4-3.  CABIN table with one cabin record

    The client locates Cabin entity beans by passing the primary key to the TravelAgentRemote’s find Cabin() method. As you saw before, this session bean interacts with the EntityManager service to find the bean in the database. The TravelAgent passes back an instance of a Cabin with that primary key. This is possible because we defined the Cabin bean class to implement the java.io.Serializable interface, which allowed the Cabin bean instance to be marshaled across the wire back to the client.

    We can now interrogate the Cabin bean instance locally to get the Cabin entity's name, deckLevel, shipId, and bedCount:

      Cabin cabin_2 = dao.findCabin(1);
      System.out.println(cabin_2.getName());
      System.out.println(cabin_2.getDeckLevel());
      System.out.println(cabin_2.getShipId());
      System.out.println(cabin_2.getBedCount());

    We are ready to create and run the Client application. Compile the Client application and deploy the Cabin entity into the container system. Then run the Client application. The output should look something like this:

      Master Suite
      1
      1
      3

    Congratulations! You just created and used your first stateless session bean and entity bean. Of course, the Client application doesn't do much, but this is a good first step to learning how to implement EJBs and entities.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT