2002 prentice hall. all rights reserved. chapter 19 enterprise java case study: business logic part...

83
2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture 19.3 ShoppingCart Implementation 19.3.1 ShoppingCart Remote Interface 19.3.2 ShoppingCartEJB Implementation 19.3.3 ShoppingCartHome Interface 19.4 Product Implementation 19.4.1 Product Remote Interface 19.4.2 ProductEJB Implementation 19.4.3 ProductHome Interface 19.4.4 ProductModel 19.5 Order Implementation 19.5.1 Order Remote Interface 19.5.2 OrderEJB Implementation 19.5.3 OrderHome Interface 19.5.4 OrderModel

Upload: berenice-wilkins

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

Chapter 19 Enterprise Java Case Study: Business Logic Part 1

Outline19.1 Introduction19.2 EJB Architecture19.3 ShoppingCart Implementation

19.3.1 ShoppingCart Remote Interface19.3.2 ShoppingCartEJB Implementation19.3.3 ShoppingCartHome Interface

19.4 Product Implementation19.4.1 Product Remote Interface19.4.2 ProductEJB Implementation19.4.3 ProductHome Interface19.4.4 ProductModel

19.5 Order Implementation19.5.1 Order Remote Interface19.5.2 OrderEJB Implementation19.5.3 OrderHome Interface19.5.4 OrderModel

Page 2: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

Chapter 19 Enterprise Java Case Study: Business Logic Part 1

19.6 OrderProduct Implementation19.6.1 OrderProduct Remote Interface19.6.2 OrderProductEJB Implementation19.6.3 OrderProductHome Interface19.6.4 OrderProductPK Primary-Key Class19.6.5 OrderProductModel

Page 3: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.1 Introduction

• Chapter presents– EJB business logic for shopping-cart e-commerce model

– entity EJBs that provide object-based interface to catalog

• You will understand – use of EJBs in e-commerce application

– more advanced EJB topics• custom primary-key classes

• many-to-many relationships

Page 4: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.2 EJB Architecture

• EJBs implement business logic of case study.• Server controller logic

– communicates with EJB business logic to• process requests

• retrieve data from database

• Entity EJBs alleviate network congestion using models to transmit EJB data.– models are Serializable classes containing all data for given EJB.

• each entity bean provides get method that returns model

Page 5: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.2 EJB Architecture (cont.)

ProductModel

GetProductServlet

getProductModel

Servlet Conta iner EJ B Container

Product EJ B

Fig. 19.1 Communication between GetProductServlet and Product EJB.

Page 6: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.3 ShoppingCart Implementation

• Stateful session EJB ShoppingCart– used by customers to gather products as they browse store

– implements business logic for managing each shopping cart

– consists of • remote interface

• EJB implementation

• home interface

– is stateful so it will persist throughout user’s session

Page 7: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.3.1 ShoppingCart Remote Interface

• Remote interface ShoppingCart– defines business logic methods available

• Each method must declare exceptions thrown.

Page 8: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.2 ShoppingCart remote interface for adding, removing and updating Products, checking out and calculating Order’s total cost.

Line 20

Lines 23-24

Lines 27-28

Lines 32-34

1 // ShoppingCart.java2 // ShoppingCart is the remote interface for stateful session3 // EJB ShoppingCart.4 package com.deitel.advjhtp1.bookstore.ejb;5 6 // Java core packages7 import java.rmi.RemoteException;8 import java.util.ArrayList;9 10 // Java extension packages11 import javax.ejb.EJBObject;12 13 // Deitel packages14 import com.deitel.advjhtp1.bookstore.model.*;15 import com.deitel.advjhtp1.bookstore.exceptions.*;16 17 public interface ShoppingCart extends EJBObject {18 19 // get contents of ShoppingCart20 public Collection getContents() throws RemoteException;21 22 // add Product with given ISBN to ShoppingCart23 public void addProduct( String isbn ) 24 throws RemoteException, ProductNotFoundException;25 26 // remove Product with given ISBN from ShoppingCart27 public void removeProduct( String isbn ) 28 throws RemoteException, ProductNotFoundException;29 30 // change quantity of Product in ShoppingCart with 31 // given ISBN to given quantity32 public void setProductQuantity( String isbn, int quantity )33 throws RemoteException, ProductNotFoundException,34 IllegalArgumentException;35

returns an Collection of Products in

ShoppingCarttakes ISBN of

product to add to ShoopingCart

removes Product with

given ISBN from ShoppingCart

updates quantity of Product with given ISNB in

ShoppingCart

Page 9: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.2 ShoppingCart remote interface for adding, removing and updating Products, checking out and calculating Order’s total cost.

Lines 37-38

Line 41

36 // checkout ShoppingCart (i.e., create new Order)37 public Order checkout( String userID ) 38 throws RemoteException, ProductNotFoundException;39 40 // get total cost for Products in ShoppingCart41 public double getTotal() throws RemoteException;42 }

places Order for Products in

ShoppingCartreturns total cost of Products in ShoppingCart

Page 10: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.3.2 ShoppingCartEJB Implementation

• ShoppingCartEJB– ShoppingCart remote interface implementation

– contains Collection of OrderProductModels.• represents an item in ShoppingCart

– each contains Product and quantity in ShoppingCart

Page 11: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface.

Line 24

Lines 27-30

Lines 33-36

1 // ShoppingCartEJB.java2 // Stateful session EJB ShoppingCart represents a Customer's 3 // shopping cart.4 package com.deitel.advjhtp1.bookstore.ejb;5 6 // Java core packages7 import java.util.*;8 import java.rmi.RemoteException;9 import java.text.DateFormat;10 11 // Java extension packages12 import javax.ejb.*;13 import javax.naming.*;14 import javax.rmi.PortableRemoteObject;15 16 // Deitel packages17 import com.deitel.advjhtp1.bookstore.model.*;18 import com.deitel.advjhtp1.bookstore.exceptions.*;19 20 public class ShoppingCartEJB implements SessionBean {21 private SessionContext sessionContext;22 23 // OrderProductModels (Products & quantities) in ShoppingCart24 private Collection orderProductModels; 25 26 // create new ShoppingCart27 public void ejbCreate() 28 { 29 orderProductModels = new ArrayList(); 30 }31 32 // get contents of ShoppingCart33 public Collection getContents() 34 { 35 return orderProductModels;

contain OrderProductModels

initializes Collection

returns contents of ShoppingCart

Page 12: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface.

Lines 44-62

Lines 54-59

36 }37 38 // add Product with given ISBN to ShoppingCart39 public void addProduct( String isbn ) 40 throws ProductNotFoundException, EJBException41 {42 // check if Product with given ISBN is already 43 // in ShoppingCart44 Iterator iterator = orderProductModels.iterator();45 46 while ( iterator.hasNext() ) {47 OrderProductModel orderProductModel = 48 ( OrderProductModel ) iterator.next();49 50 ProductModel productModel = 51 orderProductModel.getProductModel();52 53 // if Product is in ShoppingCart, increment quantity54 if ( productModel.getISBN().equals( isbn ) ) { 55 56 orderProductModel.setQuantity( 57 orderProductModel.getQuantity() + 1 );58 59 return;60 }61 62 } // end while63 64 // if Product is not in ShoppingCart, find Product with65 // given ISBN and add OrderProductModel to ShoppingCart66 try {67 InitialContext context = new InitialContext();68 69 Object object = context.lookup( 70 "java:comp/env/ejb/Product" );

if isbn found, increase corresponding quantity number

determine if product already in ShoppingCart

Page 13: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface.

Line 77

Lines 84-85

Line 87

Line 88

Line 91

Lines 96-101

71 72 ProductHome productHome = ( ProductHome )73 PortableRemoteObject.narrow( object, 74 ProductHome.class );75 76 // find Product with given ISBN77 Product product = productHome.findByPrimaryKey( isbn );78 79 // get ProductModel80 ProductModel productModel = product.getProductModel();81 82 // create OrderProductModel for ProductModel and set83 // its quantity84 OrderProductModel orderProductModel = 85 new OrderProductModel();86 87 orderProductModel.setProductModel( productModel );88 orderProductModel.setQuantity( 1 );89 90 // add OrderProductModel to ShoppingCart91 orderProductModels.add( orderProductModel );92 93 } // end try94 95 // handle exception when finding Product record96 catch ( FinderException finderException ) { 97 finderException.printStackTrace(); 98 99 throw new ProductNotFoundException( "The Product " +100 "with ISBN " + isbn + " was not found." );101 } 102 103 // handle exception when invoking Product EJB methods104 catch ( Exception exception ) { 105 throw new EJBException( exception );

locate Product EJB with given

ISBN

create OrderProductModel to store Product in ShoppingCart

add ProductModel to OrderProductModel

set quantity to 1

add OrderProductModel to Collectionif findByPrimaryKey

of interface ProductHome does not find Product with given

primary key

Page 14: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface.

Lines 114-132

Lines 126-130

Lines 135-137

106 }107 108 } // end method addProduct109 110 // remove Product with given ISBN from ShoppingCart111 public void removeProduct( String isbn ) 112 throws ProductNotFoundException 113 {114 Iterator iterator = orderProductModels.iterator();115 116 while ( iterator.hasNext() ) {117 118 // get next OrderProduct in ShoppingCart119 OrderProductModel orderProductModel = 120 ( OrderProductModel ) iterator.next();121 122 ProductModel productModel = 123 orderProductModel.getProductModel();124 125 // remove Product with given ISBN from ShoppingCart126 if ( productModel.getISBN().equals( isbn ) ) {127 orderProductModels.remove( orderProductModel );128 129 return;130 }131 132 } // end while133 134 // throw exception if Product not found in ShoppingCart135 throw new ProductNotFoundException( "The Product " +136 "with ISBN " + isbn + " was not found in your " +137 "ShoppingCart." );138 139 } // end method removeProduct140

iterate through OrderProductModels

if ProductModel found, remove

thrown when product not found

Page 15: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface.

Lines 151-154

Lines 168-171

141 // set quantity of Product in ShoppingCart 142 public void setProductQuantity( String isbn, 143 int productQuantity ) throws ProductNotFoundException 144 {145 // throw IllegalArgumentException if uantity not valid146 if ( productQuantity < 0 )147 throw new IllegalArgumentException(148 "Quantity cannot be less than zero." );149 150 // remove Product if productQuantity less than 1151 if ( productQuantity == 0 ) {152 removeProduct( isbn );153 return;154 }155 156 Iterator iterator = orderProductModels.iterator();157 158 while ( iterator.hasNext() ) {159 160 // get next OrderProduct in ShoppingCart161 OrderProductModel orderProductModel = 162 ( OrderProductModel ) iterator.next();163 164 ProductModel productModel = 165 orderProductModel.getProductModel();166 167 // set quantity for Product with given ISBN168 if ( productModel.getISBN().equals( isbn ) ) {169 orderProductModel.setQuantity( productQuantity );170 return;171 }172 173 } // end while174

removes product if product quantity is 0

updates product quantity if

product found

Page 16: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface.

Lines 176-178

Lines 192-201

175 // throw exception if Product not found in ShoppingCart176 throw new ProductNotFoundException( "The Product " +177 "with ISBN " + isbn + " was not found in your " +178 "ShoppingCart." );179 180 } // end method setProductQuantity181 182 // checkout of store (i.e., create new Order)183 public Order checkout( String userID ) 184 throws ProductNotFoundException, EJBException185 { 186 // throw exception if ShoppingCart is empty187 if ( orderProductModels.isEmpty() )188 throw new ProductNotFoundException( "There were " +189 "no Products found in your ShoppingCart." );190 191 // create OrderModel for Order details192 OrderModel orderModel = new OrderModel();193 194 // set OrderModel's date to today's Date195 orderModel.setOrderDate( new Date() );196 197 // set list of OrderProduct in OrderModel198 orderModel.setOrderProductModels( orderProductModels );199 200 // set OrderModel's shipped flag to false201 orderModel.setShipped( false );202 203 // use OrderHome interface to create new Order204 try {205 InitialContext context = new InitialContext();206 207 // look up Order EJB208 Object object = context.lookup( 209 "java:comp/env/ejb/Order" );

thrown if product not found

create OrderModel to represent Order details

Page 17: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface.

Line 217

Line 220

Line 223

210 211 OrderHome orderHome = ( OrderHome )212 PortableRemoteObject.narrow( object, 213 OrderHome.class );214 215 // create new Order using OrderModel and 216 // Customer's userID217 Order order = orderHome.create( orderModel, userID );218 219 // empty ShoppingCart for further shopping220 orderProductModels = new ArrayList();221 222 // return Order EJB that was created223 return order;224 225 } // end try226 227 // handle exception when looking up Order EJB228 catch ( Exception exception ) { 229 throw new EJBException( exception ); 230 } 231 232 } // end method checkout233 234 // get total cost for Products in ShoppingCart235 public double getTotal() 236 { 237 double total = 0.0;238 Iterator iterator = orderProductModels.iterator();239 240 // calculate Order's total cost241 while ( iterator.hasNext() ) {242

create new Order

empty ShoppingCart by creating new Collection of

OrderProductModelsreturn remote reference to newly created Order

Page 18: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface.

Lines 251-252

243 // get next OrderProduct in ShoppingCart244 OrderProductModel orderProductModel = 245 ( OrderProductModel ) iterator.next();246 247 ProductModel productModel = 248 orderProductModel.getProductModel();249 250 // add OrderProduct extended price to total251 total += ( productModel.getPrice() * 252 orderProductModel.getQuantity() );253 }254 255 return total;256 257 } // end method getTotal258 259 // set SessionContext260 public void setSessionContext( SessionContext context ) 261 { 262 sessionContext = context; 263 } 264 265 // activate ShoppingCart EJB instance 266 public void ejbActivate() {}267 268 // passivate ShoppingCart EJB instance269 public void ejbPassivate() {} 270 271 // remove ShoppingCart EJB instance272 public void ejbRemove() {}273 }

calculate total cost of items in ShoppingCart

Page 19: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.3.3 ShoppingCartHome Interface

• Interface ShoppingCartHome– defines single create method

• creates new ShoppingCart EJB instances

• EJB container provides implementation• During deployment

– use settings in following figure tables

– set Transaction Type to Required for business methods

Page 20: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.4 ShoppingCartHome interface for creating ShopppingCart EJB instances.

Lines 15-16

1 // ShoppingCartHome.java2 // ShoppingCartHome is the home interface for stateful session 3 // EJB ShoppingCart.4 package com.deitel.advjhtp1.bookstore.ejb;5 6 // Java core packages7 import java.rmi.RemoteException;8 9 // Java extension packages10 import javax.ejb.*;11 12 public interface ShoppingCartHome extends EJBHome {13 14 // create new ShoppingCart EJB15 public ShoppingCart create() 16 throws RemoteException, CreateException;17 }

implementation provided by EJB

container

Page 21: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.3.3 ShoppingCartHome Interface (cont.)

ShoppingCart General Deployment Settings

Bean Type Stateful Session

Enterprise Bean Class

com.deitel.advjhtp1.bookstore.ejb.ShoppingCartEJB

Home Interface

com.deitel.advjhtp1.bookstore.ejb.ShoppingCartHome

Remote Interface

com.deitel.advjhtp1.bookstore.ejb.ShoppingCart

Fig. 19.5 ShoppingCart general deployment settings.

Page 22: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.3.3 ShoppingCartHome Interface (cont.)

ShoppingCart EJB References

Coded Name ejb/Product

Type Entity Home com.deitel.advjhtp1.bookstore.ejb.ProductHome Remote com.deitel.advjhtp1.bookstore.ejb.Product JNDI Name Product Coded Name ejb/Order Type Entity Home com.deitel.advjhtp1.bookstore.ejb.OrderHome Remote com.deitel.advjhtp1.bookstore.ejb.Order JNDI Name Order Fig. 19.6 ShoppingCart EJ B references.

Page 23: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.4 Product Implementation

• Entity EJB Product– uses container-managed persistence

– implements methods that select, insert, update and delete database data

• Deployer provides information of Product EJB mapping to database tables.

Page 24: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.4.1 Product Remote Interface

• Declares method getProductmodel– returns ProductModel containing Product’s details

Page 25: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.7 Product remote interface for modifying details of Product EJB instances.

Lines 17-18

1 // Product.java2 // Product is the remote interface for entity EJB Product.3 package com.deitel.advjhtp1.bookstore.ejb;4 5 // Java core packages6 import java.rmi.RemoteException;7 8 // Java extension packages9 import javax.ejb.*;10 11 // Deitel packages12 import com.deitel.advjhtp1.bookstore.model.*;13 14 public interface Product extends EJBObject {15 16 // get Product details as ProductModel17 public ProductModel getProductModel() 18 throws RemoteException;19 }

enables ProductModel retrieval

Page 26: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.4.2 ProductEJB Implementation

• ProductEJB– Product remote interface implementation

– uses container-managed persistence

Page 27: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.8 ProductEJB implementation of Product remote interface.

Line 18-24

Lines 27-43

Line 30

Lines 33-39

1 // ProductEJB.java2 // Entity EJB Product represents a Product, including the 3 // ISBN, publisher, author, title, price number of pages 4 // and cover image.5 package com.deitel.advjhtp1.bookstore.ejb;6 7 // Java extension packages8 import javax.ejb.*;9 10 // Deitel packages11 import com.deitel.advjhtp1.bookstore.model.*;12 import com.deitel.advjhtp1.bookstore.*;13 14 public class ProductEJB implements EntityBean {15 private EntityContext entityContext;16 17 // container-managed fields18 public String ISBN;19 public String publisher;20 public String author;21 public String title;22 public double price;23 public int pages;24 public String image;25 26 // get Product details as ProductModel27 public ProductModel getProductModel() 28 {29 // construct new ProductModel30 ProductModel productModel = new ProductModel();31 32 // initialize ProductModel with data from Product33 productModel.setISBN( ISBN );34 productModel.setPublisher( publisher );35 productModel.setAuthor( author );

container synchronizes access to database-mapped public members

creates ProductModel containing Product’s details

create ProductModel

populate ProductModel

Page 28: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.8 ProductEJB implementation of Product remote interface.

Lines 50-56

Line 63

36 productModel.setTitle( title );37 productModel.setPrice( price );38 productModel.setPages( pages );39 productModel.setImage( image );40 41 return productModel;42 43 } // end method getProductModel44 45 // set Product details using ProductModel46 private void setProductModel( ProductModel productModel ) 47 {48 // populate Product's data members with data in 49 // provided ProductModel50 ISBN = productModel.getISBN();51 publisher = productModel.getPublisher();52 author = productModel.getAuthor();53 title = productModel.getTitle();54 price = productModel.getPrice();55 pages = productModel.getPages();56 image = productModel.getImage();57 58 } // end method setProductModel 59 60 // create instance of Product EJB using given ProductModel61 public String ejbCreate( ProductModel productModel ) 62 {63 setProductModel( productModel ); 64 return null;65 }66 67 // perform any necessary post-creation tasks68 public void ejbPostCreate( ProductModel productmodel ) {}69

set Product’s details

initialize EJB with provided

ProductModel

Page 29: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.8 ProductEJB implementation of Product remote interface.

70 // set EntityContext71 public void setEntityContext( EntityContext context ) 72 { 73 entityContext = context; 74 }75 76 // unset EntityContext77 public void unsetEntityContext() 78 { 79 entityContext = null; 80 }81 82 // activate Product EJB instance83 public void ejbActivate() 84 {85 ISBN = ( String ) entityContext.getPrimaryKey();86 }87 88 // passivate Product EJB instance89 public void ejbPassivate() 90 {91 ISBN = null;92 }93 94 // remove Product EJB instance95 public void ejbRemove() {}96 97 // store Product EJB data in database98 public void ejbStore() {}99 100 // load Product EJB data from database101 public void ejbLoad() {}102 }

Page 30: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.4.3 ProductHome Interface

• Interface ProductHome– creates new ProductEJB instances

– declares finder methods for finding existing Products.

Page 31: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.9 ProductHome interface for finding and creating Product EJB instances.

Lines 18-19

Lines 22-23

Lines 26-27

Lines 30-31

1 // ProductHome.java2 // ProductHome is the home interface for entity EJB Product.3 package com.deitel.advjhtp1.bookstore.ejb;4 5 // Java core packages6 import java.rmi.RemoteException;7 import java.util.Collection;8 9 // Java extension packages10 import javax.ejb.*;11 12 // Deitel packages13 import com.deitel.advjhtp1.bookstore.model.*;14 15 public interface ProductHome extends EJBHome {16 17 // create Product EJB using given ProductModel18 public Product create( ProductModel productModel ) 19 throws RemoteException, CreateException;20 21 // find Product with given ISBN22 public Product findByPrimaryKey( String isbn ) 23 throws RemoteException, FinderException;24 25 // find all Products26 public Collection findAllProducts() 27 throws RemoteException, FinderException;28 29 // find Products with given title30 public Collection findByTitle( String title ) 31 throws RemoteException, FinderException;32 }

interface for creating ProductEJB instance

takes the ISBN as String argument.

Container implements method using

deployment SQL queries

returns Collection of all Products in database.

Container implements method using deployment

SQL queries.

returns Collection of matching Products. Container

implements method using deployment SQL queries.

Page 32: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.4.4 ProductModel

• Implements interface Serializable– instances may be serialized over RMI-IIOP.

Page 33: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.10 ProductModel class for serializing Product data.

Line 15

Lines 18-25

1 // ProductModel.java2 // ProductModel represents a Product in the Deitel Bookstore, 3 // including ISBN, author, title and a picture of the cover.4 package com.deitel.advjhtp1.bookstore.model;5 6 // Java core packages7 import java.io.*;8 import java.util.*;9 import java.text.*;10 11 // third-party packages12 import org.w3c.dom.*;13 14 public class ProductModel implements Serializable, 15 XMLGenerator {16 17 // ProductModel properties18 private String ISBN;19 private String publisher;20 private String author;21 private String title;22 private double price;23 private int pages;24 private String image;25 private int quantity;26 27 // set ISBN28 public void setISBN( String productISBN ) 29 { 30 ISBN = productISBN; 31 }32

private members accessible through get

and set methods

implements XMLGenerator

Page 34: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.10 ProductModel class for serializing Product data.

33 // get ISBN34 public String getISBN() 35 { 36 return ISBN; 37 }38 39 // set publisher40 public void setPublisher( String productPublisher )41 { 42 publisher = productPublisher; 43 }44 45 // get publisher46 public String getPublisher()47 { 48 return publisher; 49 }50 51 // set author52 public void setAuthor( String productAuthor )53 { 54 author = productAuthor; 55 }56 57 // get author58 public String getAuthor() 59 { 60 return author; 61 }62 63 // set title64 public void setTitle( String productTitle ) 65 { 66 title = productTitle; 67 }

Page 35: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.10 ProductModel class for serializing Product data.

68 69 // get title70 public String getTitle() 71 { 72 return title; 73 }74 75 // set price76 public void setPrice( double productPrice ) 77 { 78 price = productPrice; 79 }80 81 // get price82 public double getPrice() 83 { 84 return price; 85 }86 87 // set number of pages88 public void setPages( int pageCount ) 89 { 90 pages = pageCount; 91 }92 93 // get number of pages94 public int getPages() 95 { 96 return pages; 97 }98 99 // set URL of cover image100 public void setImage( String productImage ) 101 { 102 image = productImage;

Page 36: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.10 ProductModel class for serializing Product data.

Lines 112-164

103 }104 105 // get URL of cover image106 public String getImage() 107 { 108 return image; 109 }110 111 // get XML representation of Product112 public Element getXML( Document document )113 {114 // create product Element115 Element product = document.createElement( "product" );116 117 // create ISBN Element118 Element temp = document.createElement( "ISBN" );119 temp.appendChild( 120 document.createTextNode( getISBN() ) );121 product.appendChild( temp );122 123 // create publisher Element124 temp = document.createElement( "publisher" );125 temp.appendChild( 126 document.createTextNode( getPublisher() ) );127 product.appendChild( temp );128 129 // create author Element130 temp = document.createElement( "author" );131 temp.appendChild( 132 document.createTextNode( getAuthor() ) );133 product.appendChild( temp );134

generates XML Element for data contained in

ProductModel. Does not modify Document.

Page 37: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.10 ProductModel class for serializing Product data.

Line 162

135 // create title Element136 temp = document.createElement( "title" );137 temp.appendChild( 138 document.createTextNode( getTitle() ) );139 product.appendChild( temp );140 141 NumberFormat priceFormatter =142 NumberFormat.getCurrencyInstance( Locale.US );143 144 // create price Element145 temp = document.createElement( "price" );146 temp.appendChild( document.createTextNode( 147 priceFormatter.format( getPrice() ) ) );148 product.appendChild( temp );149 150 // create pages Element151 temp = document.createElement( "pages" );152 temp.appendChild( document.createTextNode( 153 String.valueOf( getPages() ) ) );154 product.appendChild( temp );155 156 // create image Element157 temp = document.createElement( "image" );158 temp.appendChild( 159 document.createTextNode( getImage() ) );160 product.appendChild( temp );161 162 return product;163 164 } // end method getXML165 }

returns newly created Element

Page 38: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.11 XMLGenerator interface for generating XML Elements for public properties.

1 // XMLGenerator.java2 // XMLGenerator is an interface for classes that can generate3 // XML Elements. The XML element returned by method getXML4 // should contain Elements for each public property.5 package com.deitel.advjhtp1.bookstore.model;6 7 // third-party packages8 import org.w3c.dom.*;9 10 public interface XMLGenerator {11 12 // build an XML element for this Object13 public Element getXML( Document document );14 }

Page 39: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.4.4 ProductModel

Product General Deployment Settings

Bean Type Entity

Enterprise Bean Class

com.deitel.advjhtp1.bookstore.ejb.ProductEJB

Home Interface

com.deitel.advjhtp1.bookstore.ejb.ProductHome

Remote Interface

com.deitel.advjhtp1.bookstore.ejb.Product

Fig. 19.12 Product general deployment settings.

Page 40: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.4.4 ProductModel

Product Entity and Deployment Settings

Persistence Management

Container-Managed Persistence

Primary Key Class

java.lang.String

Primary Key Field Name

ISBN

Database JNDI Name

jdbc/Bookstore

Method findByTitle SQL Statement

SELECT ISBN FROM Product WHERE title LIKE ?1

Method findAllProducts SQL Statement

SELECT ISBN FROM Product WHERE 1 = 1

Fig. 19.13 Product Entity (Part 1 of 2)and deployment settings.

Page 41: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.4.4 ProductModel

Method ejbStore SQL Statement

UPDATE Product SET author = ?, image = ?, pages = ?, price = ?, publisher = ?, title = ? WHERE ISBN = ?

Method ejbCreate SQL Statement

INSERT INTO Product ( ISBN, author, image, pages, price, publisher, title ) VALUES ( ?, ?, ?, ?, ?, ?, ? )

Method ejbRemove SQL Statement

DELETE FROM Product WHERE ISBN = ?

Method findByPrimaryKey SQL Statement

SELECT ISBN FROM Product WHERE ISBN = ?

Method ejbLoad SQL Statement

SELECT author, image, pages, price, publisher, title FROM Product WHERE ISBN = ?

Table Create SQL Statement

CREATE TABLE Product ( ISBN VARCHAR( 255 ), author VARCHAR( 255 ), image VARCHAR( 255 ), pages INTEGER NOT NULL, price DOUBLE PRECISION NOT NULL, publisher VARCHAR( 255 ), title VARCHAR( 255 ), CONSTRAINT pk_Product PRIMARY KEY ( ISBN ) )

Table Delete SQL Statement

DROP TABLE Product

Fig. 19.13 (Part 2 of 2) Product Entity and deployment settings.

Page 42: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.5 Order Implementation

• Entity EJB Order– represents an order placed at Deitel Bookstore

– consists of • list of Products and associated quantities• customerID of Customer

Page 43: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.14 Order remote interface for modifying details of Order EJB instances.

Line 17

Lines 20-21

Line 24

1 // Order.java2 // Order is the remote interface for entity EJB Order.3 package com.deitel.advjhtp1.bookstore.ejb;4 5 // Java core packages6 import java.rmi.RemoteException;7 8 // Java extension packages9 import javax.ejb.*;10 11 // Deitel packages12 import com.deitel.advjhtp1.bookstore.model.*;13 14 public interface Order extends EJBObject {15 16 // get Order details as OrderModel17 public OrderModel getOrderModel() throws RemoteException;18 19 // set shipped flag20 public void setShipped( boolean flag ) 21 throws RemoteException;22 23 // get shipped flag24 public boolean isShipped() throws RemoteException;25 }

returns OrderModel containing Order details

marks Order as been shipped from warehouse

indicates Order has been shipped

Page 44: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.5.2 OrderEJB Implementation

• OrderEJB– Order remote reference implementation

– declares public members for container-managed persistence

Page 45: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.15 OrderEJB implementation of Order remote interface.

Lines 26-29

Lines 32-91

1 // OrderEJB.java2 // Entity EJB Order represents an Order, including the 3 // orderID, Order date, total cost and whether the Order4 // has shipped.5 package com.deitel.advjhtp1.bookstore.ejb;6 7 // Java core packages8 import java.util.*;9 import java.text.DateFormat;10 import java.rmi.RemoteException;11 12 // Java extension packages13 import javax.ejb.*;14 import javax.naming.*;15 import javax.rmi.PortableRemoteObject;16 17 // Deitel packages18 import com.deitel.advjhtp1.bookstore.model.*;19 20 public class OrderEJB implements EntityBean {21 private EntityContext entityContext;22 private InitialContext initialContext;23 private DateFormat dateFormat;24 25 // container-managed fields26 public Integer orderID;27 public Integer customerID;28 public String orderDate;29 public boolean shipped;30 31 // get Order details as OrderModel32 public OrderModel getOrderModel() throws EJBException33 {34 // construct new OrderModel35 OrderModel orderModel = new OrderModel();

public members for container managed persistenceconstruct OrderModel

instance containing Order details

Page 46: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.15 OrderEJB implementation of Order remote interface.

Lines 42-44

Lines 56-57

36 37 // look up OrderProduct EJB to retrieve list 38 // of Products contained in the Order 39 try {40 41 // populate OrderModel data members with data from Order42 orderModel.setOrderID( orderID );43 orderModel.setOrderDate( dateFormat.parse( orderDate ) );44 orderModel.setShipped( shipped ); 45 46 initialContext = new InitialContext();47 48 Object object = initialContext.lookup( 49 "java:comp/env/ejb/OrderProduct" );50 51 OrderProductHome orderProductHome =52 ( OrderProductHome ) PortableRemoteObject.narrow( 53 object, OrderProductHome.class );54 55 // get OrderProduct records for Order56 Collection orderProducts = 57 orderProductHome.findByOrderID( orderID );58 59 Iterator iterator = orderProducts.iterator();60 61 // OrderProductModels to place in OrderModel62 Collection orderProductModels = new ArrayList(); 63 64 // get OrderProductModel for each Product in Order 65 while ( iterator.hasNext() ) {66 OrderProduct orderProduct = ( OrderProduct ) 67 PortableRemoteObject.narrow( iterator.next(),68 OrderProduct.class );69

populate OrderModel

obtain OrderProduct records

Page 47: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.15 OrderEJB implementation of Order remote interface.

Lines 71-72

Line 76

Lines 80

70 // get OrderProductModel for OrderProduct record71 OrderProductModel orderProductModel = 72 orderProduct.getOrderProductModel();73 74 // add OrderProductModel to list of 75 // OrderProductModels in the Order76 orderProductModels.add( orderProductModel );77 }78 79 // add Collection of OrderProductModels to OrderModel80 orderModel.setOrderProductModels( orderProductModels );81 82 } // end try83 84 // handle exception working with OrderProduct EJB85 catch ( Exception exception ) { 86 throw new EJBException( exception ); 87 }88 89 return orderModel;90 91 } // end method getOrderModel92 93 // set shipped flag94 public void setShipped( boolean flag ) 95 { 96 shipped = flag; 97 }98 99 // get shipped flag100 public boolean isShipped() 101 { 102 return shipped; 103 }104

retrieve each OrderProductModel

add OrderProductModel to Collection

add Collection to OrderModel

Page 48: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.15 OrderEJB implementation of Order remote interface.

Lines 106-187

Line 128

105 // create new Order EJB using given OrderModel and userID106 public Integer ejbCreate( OrderModel order, String userID ) 107 throws CreateException 108 {109 // retrieve unique value for primary key of this110 // Order using SequenceFactory EJB111 try {112 initialContext = new InitialContext();113 114 Object object = initialContext.lookup( 115 "java:comp/env/ejb/SequenceFactory" );116 117 SequenceFactoryHome sequenceFactoryHome =118 ( SequenceFactoryHome ) 119 PortableRemoteObject.narrow( 120 object, SequenceFactoryHome.class );121 122 // find sequence for CustomerOrder table123 SequenceFactory sequenceFactory = 124 sequenceFactoryHome.findByPrimaryKey( 125 "CustomerOrders" );126 127 // get next unique orderID128 orderID = sequenceFactory.getNextID();129 130 // get date, cost, shipped flag and list of 131 // OrderProduct from provided OrderModel132 orderDate = dateFormat.format( order.getOrderDate() );133 shipped = order.getShipped();134 135 // get OrderProductModels that comprise OrderModel136 Collection orderProductModels = 137 order.getOrderProductModels();138

generates unique ID for order

creates Order EJB using data in OrderModel and userID

Page 49: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.15 OrderEJB implementation of Order remote interface.

Lines 152-162

139 // create OrderProduct EJBs for each Product in 140 // Order to keep track of quantity 141 object = initialContext.lookup( 142 "java:comp/env/ejb/OrderProduct" );143 144 OrderProductHome orderProductHome = 145 ( OrderProductHome ) PortableRemoteObject.narrow(146 object, OrderProductHome.class );147 148 Iterator iterator = orderProductModels.iterator();149 150 // create an OrderProduct EJB with Product's151 // ISBN, quantity and orderID for this Order152 while ( iterator.hasNext() ) {153 154 OrderProductModel orderProductModel = 155 ( OrderProductModel ) iterator.next();156 157 // set orderID for OrderProduct record158 orderProductModel.setOrderID( orderID );159 160 // create OrderProduct EJB instance161 orderProductHome.create( orderProductModel );162 }163 164 // get customerID for customer placing Order165 object = initialContext.lookup( 166 "java:comp/env/ejb/Customer" );167 168 CustomerHome customerHome = 169 ( CustomerHome ) PortableRemoteObject.narrow( 170 object, CustomerHome.class );171

create OrderProduct records

Page 50: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.15 OrderEJB implementation of Order remote interface.

Lines 173-174

Line 176

172 // use provided userID to find Customer173 Customer customer = 174 customerHome.findByUserID( userID );175 176 customerID = ( Integer ) customer.getPrimaryKey();177 178 } // end try179 180 // handle exception when looking up EJBs181 catch ( Exception exception ) { 182 throw new CreateException( exception.getMessage() ); 183 }184 185 return null;186 187 } // end method ejbCreate188 189 // perform any necessary post-creation tasks190 public void ejbPostCreate( OrderModel order, String id ) {}191 192 // set EntityContext193 public void setEntityContext( EntityContext context ) 194 { 195 entityContext = context; 196 dateFormat = DateFormat.getDateTimeInstance( 197 DateFormat.FULL, DateFormat.SHORT, Locale.US );198 }199 200 // unset EntityContext201 public void unsetEntityContext() 202 { 203 entityContext = null; 204 }205

retrieve Customer EJB for given userID

set customerID for Order

Page 51: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.15 OrderEJB implementation of Order remote interface.

206 // activate Order EJB instance207 public void ejbActivate() 208 {209 orderID = ( Integer ) entityContext.getPrimaryKey();210 }211 212 // passivate Order EJB instance213 public void ejbPassivate() 214 {215 orderID = null;216 }217 218 // remove Order EJB instanceCus219 public void ejbRemove() {}220 221 // store Order EJB data in database222 public void ejbStore() {}223 224 // load Order EJB data from database225 public void ejbLoad() {}226 }

Page 52: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.5.3 OrderHome Interface

• Creates Order instances.• Finds existing Order instances.

Page 53: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.16 OrderHome interface for finding and creating Order EJB instances.

Lines 18-19

Lines 22-23

Lines 26-27

1 // OrderHome.java2 // OrderHome is the home interface for entity EJB Order.3 package com.deitel.advjhtp1.bookstore.ejb;4 5 // Java core packages6 import java.util.*;7 import java.rmi.RemoteException;8 9 // Java extension packages10 import javax.ejb.*;11 12 // Deitel packages13 import com.deitel.advjhtp1.bookstore.model.*;14 15 public interface OrderHome extends EJBHome {16 17 // create Order using given OrderModel and userID18 public Order create( OrderModel orderModel, String userID )19 throws RemoteException, CreateException;20 21 // find Order using given orderID22 public Order findByPrimaryKey( Integer orderID ) 23 throws RemoteException, FinderException;24 25 // find Orders for given customerID26 public Collection findByCustomerID( Integer customerID )27 throws RemoteException, FinderException;28 }

creates new Order

locates existing Order

retrieves Collection of Orders for given

Customer

Page 54: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.5.4 OrderModel

• Encapsulates details of an Order EJB in Serializable object.

Page 55: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.17 OrderModel class for serializing Order data.

Lines 19-22

1 // OrderModel.java2 // OrderModel represents an Order and contains the order ID, 3 // date, total cost and a boolean indicating whether or not the4 // order has shipped.5 package com.deitel.advjhtp1.bookstore.model;6 7 // Java core packages8 import java.io.*;9 import java.util.*;10 import java.text.*;11 12 // third-party packages13 import org.w3c.dom.*;14 15 public class OrderModel implements Serializable, 16 XMLGenerator {17 18 // OrderModel properties19 private Integer orderID;20 private Date orderDate;21 private boolean shipped;22 private Collection orderProductModels;23 24 // construct empty OrderModel25 public OrderModel()26 {27 orderProductModels = new ArrayList();28 }29 30 // set order ID31 public void setOrderID( Integer id ) 32 { 33 orderID = id; 34 }35

have associated set and get methods

Page 56: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.17 OrderModel class for serializing Order data.

36 // get order ID37 public Integer getOrderID() 38 { 39 return orderID; 40 }41 42 // set order date43 public void setOrderDate( Date date ) 44 { 45 orderDate = date; 46 }47 48 // get order date49 public Date getOrderDate() 50 { 51 return orderDate; 52 }53 54 // get total cost55 public double getTotalCost() 56 { 57 double total = 0.0;58 59 Iterator iterator = orderProductModels.iterator();60 61 // calculate Order's total cost62 while ( iterator.hasNext() ) {63 64 // get next OrderProduct in ShoppingCart65 OrderProductModel orderProductModel = 66 ( OrderProductModel ) iterator.next();67 68 ProductModel productModel = 69 orderProductModel.getProductModel();70

Page 57: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.17 OrderModel class for serializing Order data.

71 // add OrderProduct extended price to total72 total += ( productModel.getPrice() * 73 orderProductModel.getQuantity() );74 }75 76 return total;77 }78 79 // set shipped flag80 public void setShipped( boolean orderShipped ) 81 { 82 shipped = orderShipped; 83 }84 85 // get shipped flag86 public boolean getShipped() 87 { 88 return shipped; 89 }90 91 // set list of OrderProductModels92 public void setOrderProductModels( Collection models )93 { 94 orderProductModels = models; 95 }96 97 // get OrderProductModels98 public Collection getOrderProductModels() 99 { 100 return Collections.unmodifiableCollection( 101 orderProductModels );102 }103

Page 58: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.17 OrderModel class for serializing Order data.

Lines 105-166

104 // get XML representation of Order105 public Element getXML( Document document )106 {107 // create order Element108 Element order = document.createElement( "order" );109 110 // create orderID Element111 Element temp = document.createElement( "orderID" );112 temp.appendChild( document.createTextNode( 113 String.valueOf( getOrderID() ) ) );114 order.appendChild( temp );115 116 // get DateFormat for writing Date to XML document117 DateFormat formatter = DateFormat.getDateTimeInstance(118 DateFormat.DEFAULT, DateFormat.MEDIUM, Locale.US );119 120 // create orderDate Element121 temp = document.createElement( "orderDate" );122 temp.appendChild( document.createTextNode( 123 formatter.format( getOrderDate() ) ) );124 order.appendChild( temp );125 126 NumberFormat costFormatter =127 NumberFormat.getCurrencyInstance( Locale.US );128 129 // create totalCost Element130 temp = document.createElement( "totalCost" );131 temp.appendChild( document.createTextNode( 132 costFormatter.format( getTotalCost() ) ) );133 order.appendChild( temp );134 135 // create shipped Element136 temp = document.createElement( "shipped" );137

from interface XMLGenerator

Page 59: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.17 OrderModel class for serializing Order data.

138 if ( getShipped() )139 temp.appendChild( 140 document.createTextNode( "yes" ) );141 else142 temp.appendChild( 143 document.createTextNode( "no" ) );144 145 order.appendChild( temp );146 147 // create orderProducts Element148 Element orderProducts = 149 document.createElement( "orderProducts" );150 151 Iterator iterator = getOrderProductModels().iterator();152 153 // add orderProduct element for each OrderProduct154 while ( iterator.hasNext() ) {155 OrderProductModel orderProductModel = 156 ( OrderProductModel ) iterator.next();157 158 orderProducts.appendChild( 159 orderProductModel.getXML( document ) );160 }161 162 order.appendChild( orderProducts );163 164 return order;165 166 } // end method getXML167 }

Page 60: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.5.4 OrderModel (cont.)

Order General Deployment Settings

Bean Type Entity

Enterprise Bean Class

com.deitel.advjhtp1.bookstore.ejb.OrderEJB

Home Interface

com.deitel.advjhtp1.bookstore.ejb.OrderHome

Remote Interface

com.deitel.advjhtp1.bookstore.ejb.Order

Fig. 19.18 Order general deployment settings.

Page 61: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.5.4 OrderModel (cont.)

Order Entity and Deployment Settings

Persistence Management

Container-Managed Persistence

Primary Key Class java.lang.Integer

Primary Key Field Name

orderID

Database JNDI Name

jdbc/Bookstore

Method findByCustomerID SQL Statement

SELECT orderID FROM CustomerOrders WHERE customerID = ?1

Method ejbStore SQL Statement

UPDATE CustomerOrders SET customerID = ?, orderDate = ?, shipped = ? WHERE orderID = ?

Method ejbCreate SQL Statement

INSERT INTO CustomerOrders ( customerID, orderDate, orderID, shipped ) VALUES ( ?, ?, ?, ? )

Method ejbRemove SQL Statement

DELETE FROM CustomerOrders WHERE orderID = ?

Method findByPrimaryKey SQL Statement

SELECT orderID FROM CustomerOrders WHERE orderID = ?

Method ejbLoad SQL Statement

SELECT customerID, orderDate, shipped FROM CustomerOrders WHERE orderID = ?

Table Create SQL Statement

CREATE TABLE CustomerOrders ( customerID INTEGER, orderDate VARCHAR(255), orderID INTEGER, shipped BOOLEAN NOT NULL, CONSTRAINT pk_CustomerOrders PRIMARY KEY ( orderID ) )

Table Delete SQL Statement

DROP TABLE CustomerOrders

Fig. 19.19 Order entity and deployment settings.

Page 62: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.5.4 OrderModel (cont.)

Order EJB References

Coded Name

ejb/Product

Type Entity Home com.deitel.advjhtp1.bookstore.ejb.ProductHome Remote com.deitel.advjhtp1.bookstore.ejb.Product JNDI Name

Product

Coded Name

ejb/SequenceFactory

Type Entity Home com.deitel.advjhtp1.bookstore.ejb.SequenceFactoryHome

Remote com.deitel.advjhtp1.bookstore.ejb.SequenceFactory JNDI Name

SequenceFactory

Coded Name

ejb/Customer

Type Entity

Home com.deitel.advjhtp1.bookstore.ejb.CustomerHome Remote com.deitel.advjhtp1.bookstore.ejb.Customer JNDI Name

Customer

Coded Name

ejb/OrderProduct

Type Entity Home com.deitel.advjhtp1.bookstore.ejb.OrderProductHome Remote com.deitel.advjhtp1.bookstore.ejb.OrderProduct JNDI Name

OrderProduct

Fig. 19.20 Order EJ B references.

Page 63: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6 OrderProduct Implementation

• EJB OrderProduct– Many-to-many relationship between Orders and Products

Page 64: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.1 OrderProduct Remote Interface

• OrderProduct remote interface– Business methods for the OrderProduct EJB

Page 65: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.21 OrderProduct remote interface for modifying details of OrderProduct EJB instances.

Lines 18-19

1 // OrderProduct.java2 // OrderProduct is the remote interface for entity EJB3 // OrderProduct.4 package com.deitel.advjhtp1.bookstore.ejb;5 6 // Java core packages7 import java.rmi.RemoteException;8 9 // Java extension packages10 import javax.ejb.*;11 12 // Deitel packages13 import com.deitel.advjhtp1.bookstore.model.*;14 15 public interface OrderProduct extends EJBObject {16 17 // get OrderProduct details as OrderProductModel18 public OrderProductModel getOrderProductModel() 19 throws RemoteException;20 }

Method getOrderProductModel returns an OrderProductModel that contains the details of an OrderProduct record.

Page 66: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.2 OrderProductEJB Implementation

• OrderProductEJB– Implements OrderProduct remote interface

Page 67: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.22 OrderProductEJB implementation of OrderProduct remote interface.

Lines 22-24

Lines 27-69

1 // OrderProductEJB.java2 // Entity EJB OrderProductEJB represents the mapping between3 // a Product and an Order, including the quantity of the 4 // Product in the Order.5 package com.deitel.advjhtp1.bookstore.ejb;6 7 // Java core packages8 import java.rmi.RemoteException;9 10 // Java extension packages11 import javax.ejb.*;12 import javax.naming.*;13 import javax.rmi.PortableRemoteObject;14 15 // Deitel packages16 import com.deitel.advjhtp1.bookstore.model.*;17 18 public class OrderProductEJB implements EntityBean {19 private EntityContext entityContext;20 21 // container-managed fields22 public String ISBN;23 public Integer orderID;24 public int quantity;25 26 // get OrderProduct details as OrderProductModel27 public OrderProductModel getOrderProductModel()28 throws EJBException29 {30 OrderProductModel model = new OrderProductModel();31 32 // get ProductModel for Product in this OrderProduct33 try {34 Context initialContext = new InitialContext();35

Declare container-managed fields ISBN, orderID and quantity.

Method getOrderProductModel returns the details of the OrderProduct record as an OrderProductModel.

Page 68: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.22 OrderProductEJB implementation of OrderProduct remote interface.

36 // look up Product EJB37 Object object = initialContext.lookup( 38 "java:comp/env/ejb/Product" );39 40 // get ProductHome interface41 ProductHome productHome = ( ProductHome ) 42 PortableRemoteObject.narrow( object, 43 ProductHome.class );44 45 // find Product using its ISBN46 Product product = 47 productHome.findByPrimaryKey( ISBN );48 49 // get ProductModel50 ProductModel productModel = 51 product.getProductModel();52 53 // set ProductModel in OrderProductModel54 model.setProductModel( productModel );55 56 } // end try57 58 // handle exception when looking up Product EJB59 catch ( Exception exception ) { 60 throw new EJBException( exception ); 61 }62 63 // set orderID and quantity in OrderProductModel64 model.setOrderID( orderID );65 model.setQuantity( quantity );66 67 return model;68 69 } // end method getOrderProductModel70

Page 69: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.22 OrderProductEJB implementation of OrderProduct remote interface.

Lines 72-75

Lines 80-84

Line 82

71 // set OrderProduct details using OrderProductModel72 private void setOrderProductModel( OrderProductModel model )73 { 74 ISBN = model.getProductModel().getISBN();75 orderID = model.getOrderID();76 quantity = model.getQuantity();77 } 78 79 // create OrderProduct for given OrderProductModel80 public OrderProductPK ejbCreate( OrderProductModel model )81 {82 setOrderProductModel( model );83 return null;84 }85 86 // perform any necessary post-creation tasks87 public void ejbPostCreate( OrderProductModel model ) {}88 89 // set EntityContext90 public void setEntityContext( EntityContext context ) 91 { 92 entityContext = context; 93 }94 95 // unset EntityContext96 public void unsetEntityContext() 97 { 98 entityContext = null; 99 }100 101 // activate OrderProduct EJB instance102 public void ejbActivate() 103 {104 OrderProductPK primaryKey = 105 ( OrderProductPK ) entityContext.getPrimaryKey();

Set the details of the OrderProduct record, using data from the OrderProductModel argument.

The EJB container calls method ejbCreate to create new instances of the OrderProduct EJB.

Invokes method setOrderProductModel to complete the creation of the OrderProduct record.

Page 70: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.22 OrderProductEJB implementation of OrderProduct remote interface.

106 107 ISBN = primaryKey.getISBN();108 orderID = primaryKey.getOrderID();109 }110 111 // passivate OrderProduct EJB instance112 public void ejbPassivate() 113 {114 ISBN = null;115 orderID = null;116 }117 118 // remove OrderProduct EJB instance119 public void ejbRemove() {}120 121 // store OrderProduct EJB data in database122 public void ejbStore() {}123 124 // load OrderProduct EJB data from database125 public void ejbLoad() {}126 }

Page 71: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.3 OrderProductHome Interface

• OrderProductHome interface– Creates new OrderProduct EJB instance

– Locates existing OrderProduct records

Page 72: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.23 OrderProductHome interface for finding and creating OrderProduct EJB instances.

Lines 19-20

Lines 23-24

Lines 27-28

1 // OrderProductHome.java2 // OrderProductHome is the home interface for entity EJB3 // OrderProduct.4 package com.deitel.advjhtp1.bookstore.ejb;5 6 // Java core packages7 import java.util.Collection;8 import java.rmi.RemoteException;9 10 // Java extension packages11 import javax.ejb.*;12 13 // Deitel packages14 import com.deitel.advjhtp1.bookstore.model.*;15 16 public interface OrderProductHome extends EJBHome {17 18 // create OrderProduct using given OrderProductModel19 public OrderProduct create( OrderProductModel model ) 20 throws RemoteException, CreateException;21 22 // find OrderProduct for given orderID23 public Collection findByOrderID( Integer orderID ) 24 throws RemoteException, FinderException;25 26 // find OrderProduct for given primary key27 public OrderProduct findByPrimaryKey( OrderProductPK pk )28 throws RemoteException, FinderException;29 }

Method create corresponds to method ejbCreate of the OrderProductEJB implementation.locates all OrderProduct

records with the provided orderID and returns a Collection of OrderProduct remote references.

Locates an OrderProduct record, using an instance of the OrderProductPK primary-key class.

Page 73: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.4 OrderProductPK Primary-Key Class

• OrderProductPK– Primary-key class for the OrderProduct EJB

Page 74: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.23 OrderProductPK primary-key class for OrderProduct EJB.

Lines 12-13

1 // OrderProductPK.java2 // OrderProductPK is a primary-key class for entity EJB 3 // OrderProduct.4 package com.deitel.advjhtp1.bookstore.ejb;5 6 // Java core packages7 import java.io.*;8 9 public class OrderProductPK implements Serializable {10 11 // primary-key fields12 public String ISBN;13 public Integer orderID;14 15 // no-argument constructor16 public OrderProductPK() {} 17 18 // construct OrderProductPK with ISBN and orderID19 public OrderProductPK( String isbn, Integer id )20 {21 ISBN = isbn;22 orderID = id;23 }24 25 // get ISBN 26 public String getISBN() 27 { 28 return ISBN; 29 }30 31 // get orderID32 public Integer getOrderID() 33 { 34 return orderID; 35 }

Two public data members ISBN and orderID correspond to OrderProductEJB’s two primary-key fields.

Page 75: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.23 OrderProductPK primary-key class for OrderProduct EJB.

Lines 38-41

Lines 44-57

36 37 // calculate hashCode for this Object38 public int hashCode() 39 { 40 return getISBN().hashCode() ^ getOrderID().intValue(); 41 }42 43 // custom implementation of Object equals method44 public boolean equals( Object object ) 45 {46 // ensure object is instance of OrderProductPK47 if ( object instanceof OrderProductPK ) { 48 OrderProductPK otherKey = 49 ( OrderProductPK ) object;50 51 // compare ISBNs and orderIDs52 return ( getISBN().equals( otherKey.getISBN() ) 53 && getOrderID().equals( otherKey.getOrderID() ) );54 }55 56 return false;57 }58 }

The overridden implementations of methods hasCode and equals enable the EJB container and OrderProduct EJB clients to determine if two OrderProduct EJB instances are equal by comparing their primary-key class instances.

Page 76: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.5 OrderProductModel

• OrderProductModel– Represents an OrderProduct record

Page 77: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.25 OrderProductModel class for serializing OrderProduct data.

1 // OrderProductModel.java2 // OrderProductModel represents a Product and its quantity in 3 // an Order or ShoppingCart.4 package com.deitel.advjhtp1.bookstore.model;5 6 // Java core packages7 import java.io.*;8 import java.util.*;9 import java.text.*;10 11 // third-party packages12 import org.w3c.dom.*;13 14 public class OrderProductModel implements Serializable, 15 XMLGenerator {16 17 // OrderProductModel properties18 private ProductModel productModel;19 private int quantity;20 private Integer orderID;21 22 // set ProductModel23 public void setProductModel( ProductModel model ) 24 { 25 productModel = model;26 }27 28 // get ProductModel29 public ProductModel getProductModel() 30 { 31 return productModel;32 }33

Page 78: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.25 OrderProductModel class for serializing OrderProduct data.

34 // set quantity35 public void setQuantity( int productQuantity )36 {37 quantity = productQuantity;38 }39 40 // get quantity41 public int getQuantity()42 {43 return quantity;44 }45 46 // set orderID47 public void setOrderID( Integer id )48 {49 orderID = id;50 }51 52 // get orderID53 public Integer getOrderID()54 {55 return orderID;56 }57 58 // get XML representation of OrderProduct59 public Element getXML( Document document )60 {61 // create orderProduct Element62 Element orderProduct = 63 document.createElement( "orderProduct" );64 65 // append ProductModel product Element66 orderProduct.appendChild ( 67 getProductModel().getXML( document ) );68

Page 79: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall.All rights reserved.

Outline

Fig. 19.25 OrderProductModel class for serializing OrderProduct data.

69 // create quantity Element70 Element temp = document.createElement( "quantity" ); 71 temp.appendChild( document.createTextNode( 72 String.valueOf ( getQuantity() ) ) ); 73 orderProduct.appendChild( temp );74 75 return orderProduct;76 }77 }

Page 80: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.5 OrderProductModel (Cont.)

OrderProduct General Deployment Settings

Bean Type Entity

Enterprise Bean Class

com.deitel.advjhtp1.bookstore.ejb.OrderProductEJB

Home Interface

com.deitel.advjhtp1.bookstore.ejb.OrderProductHome

Remote Interface

com.deitel.advjhtp1.bookstore.ejb.OrderProduct

Fig. 19.26 OrderProduct general deployment settings.

Page 81: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.5 OrderProductModel (Cont.)

OrderProduct Entity and Deployment Settings

Persistence Management

Container-Managed Persistence

Primary Key Class

com.deitel.advjhtp1.bookstore.ejb.OrderProductPK

Primary Key Field Name

N/A

Database JNDI Name

jdbc/Bookstore

Method findByOrderID SQL Statement

SELECT ISBN, orderID FROM OrderProduct WHERE orderID = ?1

Method ejbStore SQL Statement

UPDATE OrderProduct SET quantity = ? WHERE ISBN = ? AND orderID = ?

Fig. 19.27 (Part 1 of 2) OrderProduct entity and deployment settings.

Page 82: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.5 OrderProductModel (Cont.)

Method ejbCreate SQL Statement

INSERT INTO OrderProduct ( ISBN, orderID, quantity ) VALUES ( ?, ?, ? )

Method ejbRemove SQL Statement

DELETE FROM OrderProduct WHERE ISBN = ? AND orderID = ?

Method findByPrimaryKey SQL Statement

SELECT ISBN, orderID FROM OrderProduct WHERE ISBN = ? AND orderID = ?

Method ejbLoad SQL Statement

SELECT quantity FROM OrderProduct WHERE ISBN = ? AND orderID = ?

Table Create SQL Statement

CREATE TABLE OrderProduct ( ISBN VARCHAR( 255 ), orderID INTEGER, quantity INTEGER NOT NULL, CONSTRAINT pk_OrderProduct PRIMARY KEY ( ISBN, orderID ) )

Table Delete SQL Statement

DROP TABLE OrderProduct

Fig. 19.27 (Part 2 of 2) OrderProduct entity and deployment settings.

Page 83: 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture

2002 Prentice Hall. All rights reserved.

19.6.5 OrderProductModel (Cont.)

OrderProduct EJB References

Coded Name ejb/Product

Type Entity Home com.deitel.advjhtp1.bookstore.ejb.ProductHome Remote com.deitel.advjhtp1.bookstore.ejb.Product JNDI Name Product Fig. 19.28 OrderProduct EJ B references.