Home arrow Java arrow Page 5 - Developing Your First Beans
JAVA

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: 4 stars4 stars4 stars4 stars4 stars / 3
November 09, 2006
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
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...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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