Chapter 15: Entity EJBs
Outline
15.1 Introduction
15.2 Entity EJB Overview 15.3 Employee Entity EJB
15.4 Employee EJB Home and Remote Interfaces 15.5 Employee EJB with Bean-Managed Persistence
15.5.1 Employee EJB Implementation 15.5.2 Employee EJB Deployment
15.6 Employee EJB with Container-Managed Persistence 15.7 Employee EJB Client
15.1 Introduction
• Entity EJBs
– Object-based representations of information-tier data
• e.g., data stored in relational database
– We present two versions:
• Demonstrate entity EJB that uses JDBC to persist data
• Demonstrate how container manages data storage and retrieval
15.2 Entity EJB Overview
• Entity EJB
– Represents a particular unit of data
• e.g., record in a database table
– Two types:
• Bean-managed persistence
• Container-managed persistence
15.3 Employee Entity EJB
• Build EJB that represents and Employee
– Two implementations
• Bean-managed persistence
• Container-managed persistence
15.4 Employee EJB Home and Remote Interfaces
• EJB home interface
– Includes create methods and finder methods
• EJB remote interface
– Includes methods, such as for setting and getting information
2002 Prentice Hall.
Outline
Fig. 15.1 Employee remote interface for setting and getting Employee
information (part 1).
Line 11 Lines 14-33
1 // Employee.java
2 // Employee is the remote interface for the Address EJB.
3 package com.deitel.advjhtp1.ejb.entity;
4
5 // Java core libraries
6 import java.rmi.RemoteException;
7
8 // Java standard extensions 9 import javax.ejb.EJBObject;
10
11 public interface Employee extends EJBObject { 12
13 // get Employee ID
14 public Integer getEmployeeID() throws RemoteException;
15
16 // set social security number
17 public void setSocialSecurityNumber( String number ) 18 throws RemoteException;
19
20 // get social security number
21 public String getSocialSecurityNumber() 22 throws RemoteException;
23
24 // set first name
25 public void setFirstName( String name ) 26 throws RemoteException;
27
28 // get first name
29 public String getFirstName() throws RemoteException;
30
31 // set last name
32 public void setLastName( String name ) 33 throws RemoteException;
34
All EJB remote interfaces must extend interface EJBObject
Interface Employee provides access methods for
each Employee property
Outline
Fig. 15.1 Employee remote interface for setting and getting Employee
information (part 2).
Lines 36-49
35 // get last name
36 public String getLastName() throws RemoteException;
37
38 // set title
39 public void setTitle( String title ) 40 throws RemoteException;
41
42 // get title
43 public String getTitle() throws RemoteException;
44
45 // set salary
46 public void setSalary( Double salary ) throws RemoteException;
47
48 // get salary
49 public Double getSalary() throws RemoteException;
50 }
Interface Employee provides access methods for
each Employee property
2002 Prentice Hall.
Outline
Fig. 15.2
EmployeeHome interface for finding and creating
Employee EJBs.
Line 12 Lines 15-20
1 // EmployeeHome.java
2 // EmployeeHome is the home interface for the Employee EJB.
3 package com.deitel.advjhtp1.ejb.entity;
4
5 // Java core libraries 6 import java.rmi.*;
7 import java.util.*;
8
9 // Java standard extensions 10 import javax.ejb.*;
11
12 public interface EmployeeHome extends EJBHome { 13
14 // find Employee with given primary key
15 public Employee findByPrimaryKey( Integer primaryKey ) 16 throws RemoteException, FinderException;
17
18 // create Employee EJB
19 public Employee create( Integer primaryKey ) 20 throws RemoteException, CreateException;
21 }
All EJB home interfaces must extend
interface EJBHome
Interface EmployeeHome provides finder and create methods
15.5 Employee EJB with Bean- Managed Persistence
• Employee EJB
– Bean-managed persistence
• Use JDBC to store Employee data in an underlying database
– Deploying the EJB
15.5.1 Employee EJB Implementation
• Employee EJB implementation
– Bean-managed persistence
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 1).
Line 15 Line 17 Lines 20-25
1 // EmployeeEJB.java
2 // EmployeeEJB is an entity EJB that uses bean-managed 3 // persistence to persist Employee data in a database.
4 package com.deitel.advjhtp1.ejb.entity.bmp;
5
6 // Java core libraries 7 import java.sql.*;
8 import java.rmi.RemoteException;
9
10 // Java standard extensions 11 import javax.ejb.*;
12 import javax.sql.*;
13 import javax.naming.*;
14
15 public class EmployeeEJB implements EntityBean { 16
17 private EntityContext entityContext;
18 private Connection connection;
19
20 private Integer employeeID;
21 private String socialSecurityNumber;
22 private String firstName;
23 private String lastName;
24 private String title;
25 private Double salary;
26
27 // get Employee ID
28 public Integer getEmployeeID() 29 {
30 return employeeID;
31 } 32
All EJB implementations must implement interface EntityBean
EntityContext provides EJB with information about container that deploys the EJB
Member variables to store data retrieved from the database and
updates from the client
2002 Prentice Hall.
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 2).
33 // set social security number
34 public void setSocialSecurityNumber( String number ) 35 {
36 socialSecurityNumber = number;
37 } 38
39 // get social security number
40 public String getSocialSecurityNumber() 41 {
42 return socialSecurityNumber;
43 } 44
45 // set first name
46 public void setFirstName( String name ) 47 {
48 firstName = name;
49 } 50
51 // get first name
52 public String getFirstName() 53 {
54 return firstName;
55 } 56
57 // set last name
58 public void setLastName( String name ) 59 {
60 lastName = name;
61 } 62
63 // get last name
64 public String getLastName() 65 {
66 return lastName;
67 }
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 3).
Line 94
68
69 // set title
70 public void setTitle( String jobTitle ) 71 {
72 title = jobTitle;
73 } 74
75 // get title
76 public String getTitle() 77 {
78 return title;
79 } 80
81 // set salary
82 public void setSalary( Double amount ) 83 {
84 salary = amount;
85 } 86
87 // get salary
88 public Double getSalary() 89 {
90 return salary;
91 } 92
93 // create Employee
94 public Integer ejbCreate( Integer primaryKey ) 95 throws CreateException
96 {
97 employeeID = primaryKey;
98
When a client invokes interface
EmployeeHome method create, the EJB container invokes method ejbCreate
2002 Prentice Hall.
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 4).
Lines 103-111 Line 114
Line 127 Line 130
99 // INSERT new Employee in database 100 try {
101
102 // create INSERT statement
103 String insert = "INSERT INTO Employee " + 104 "( employeeID ) VALUES ( ? )";
105
106 // create PreparedStatement to perform INSERT 107 PreparedStatement insertStatement =
108 connection.prepareStatement( insert );
109
110 // set values for PreparedStatement
111 insertStatement.setInt( 1, employeeID.intValue() );
112
113 // execute INSERT and close PreparedStatement 114 insertStatement.executeUpdate();
115 insertStatement.close();
116
117 return employeeID;
118 } 119
120 // throw EJBException if INSERT fails 121 catch ( SQLException sqlException ) {
122 throw new CreateException( sqlException.getMessage() );
123 }
124 } // end method ejbCreate 125
126 // do post-creation tasks when creating Employee 127 public void ejbPostCreate( Integer primaryKey ) {}
128
129 // remove Employee information from database 130 public void ejbRemove() throws RemoveException 131 {
Create a PreparedStatement to INSERT the new Employee in
the database
INSERT the Employee in the database
EJB container invokes method ejbPostCreate after invoking method ejbCreate
to perform required tasks
When a client invokes interface
EmployeeHome method remove, the EJB container invokes method ejbRemove
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 5).
Lines 144-145 Line 151
Line 162
132 // DELETE Employee record 133 try {
134
135 // get primary key of Employee to be removed 136 Integer primaryKey =
137 ( Integer ) entityContext.getPrimaryKey();
138
139 // create DELETE statement
140 String delete = "DELETE FROM Employee WHERE " + 141 "employeeID = ?";
142
143 // create PreparedStatement to perform DELETE 144 PreparedStatement deleteStatement =
145 connection.prepareStatement( delete );
146
147 // set values for PreparedStatement
148 deleteStatement.setInt( 1, primaryKey.intValue() );
149
150 // execute DELETE and close PreparedStatement 151 deleteStatement.executeUpdate();
152 deleteStatement.close();
153 } 154
155 // throw new EJBException if DELETE fails 156 catch ( SQLException sqlException ) {
157 throw new RemoveException( sqlException.getMessage() );
158 }
159 } // end method ejbRemove 160
161 // store Employee information in database 162 public void ejbStore() throws EJBException 163 {
Create a Prepared- Statement to DELETE
the Employee from the database
DELETE the Employee from the database
EJB container invokes ejbStore to save Employee data in the
database
2002 Prentice Hall.
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 6).
Lines 178-179 Line 190
164 // UPDATE Employee record 165 try {
166
167 // get primary key for Employee to be updated 168 Integer primaryKey =
169 ( Integer ) entityContext.getPrimaryKey();
170
171 // create UPDATE statement
172 String update = "UPDATE Employee SET " +
173 "socialSecurityNumber = ?, firstName = ?, " + 174 "lastName = ?, title = ?, salary = ? " +
175 "WHERE employeeID = ?";
176
177 // create PreparedStatement to perform UPDATE 178 PreparedStatement updateStatement =
179 connection.prepareStatement( update );
180
181 // set values in PreparedStatement
182 updateStatement.setString( 1,socialSecurityNumber);
183 updateStatement.setString( 2,firstName );
184 updateStatement.setString( 3,lastName );
185 updateStatement.setString( 4,title );
186 updateStatement.setDouble( 5,salary.doubleValue());
187 updateStatement.setInt( 6, primaryKey.intValue() );
188
189 // execute UPDATE and close PreparedStatement 190 updateStatement.executeUpdate();
191 updateStatement.close();
192 } 193
194 // throw EJBException if UPDATE fails 195 catch ( SQLException sqlException ) {
196 throw new EJBException( sqlException );
197 }
198 } // end method ejbStore
Create a Prepared- Statement to UPDATE
the Employee information in the database
UPDATE the Employee information in the database
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 7).
Line 201
Lines 215-216 Line 222
199
200 // load Employee information from database 201 public void ejbLoad() throws EJBException 202 {
203 // get Employee record from Employee database table 204 try {
205
206 // get primary key for Employee to be loaded 207 Integer primaryKey =
208 ( Integer ) entityContext.getPrimaryKey();
209
210 // create SELECT statement
211 String select = "SELECT * FROM Employee WHERE " + 212 "employeeID = ?";
213
214 // create PreparedStatement for SELECT 215 PreparedStatement selectStatement =
216 connection.prepareStatement( select );
217
218 // set employeeID value in PreparedStatement
219 selectStatement.setInt( 1, primaryKey.intValue() );
220
221 // execute selectStatement
222 ResultSet resultSet = selectStatement.executeQuery();
223
224 // get Employee information from ResultSet and update 225 // local member variables to cache data
226 if ( resultSet.next() ) { 227
228 // get employeeID
229 employeeID = new Integer( resultSet.getInt(
230 "employeeID" ) );
231
EJB container invokes ejbLoad to copy Employee
data from the database to Employee member variables
Create a Prepared- Statement to SELECT
the Employee information in the database
SELECT the Employee information in the database
2002 Prentice Hall.
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 8).
Lines 233-247
232 // get social-security number
233 socialSecurityNumber = resultSet.getString(
234 "socialSecurityNumber" );
235
236 // get first name
237 firstName = resultSet.getString( "firstName" );
238
239 // get last name
240 lastName = resultSet.getString( "lastName" );
241
242 // get job title
243 title = resultSet.getString( "title" );
244
245 // get salary
246 salary = new Double( resultSet.getDouble(
247 "salary" ) );
248
249 } // end if 250
251 else
252 throw new EJBException( "No such employee." );
253
254 // close PreparedStatement 255 selectStatement.close();
256
257 } // end try 258
259 // throw EJBException if SELECT fails 260 catch ( SQLException sqlException ) {
261 throw new EJBException( sqlException );
262 }
263 } // end method ejbLoad 264
Store database data in Employee member variables
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 9).
Line 266
Lines 273-284
265 // find Employee using its primary key
266 public Integer ejbFindByPrimaryKey( Integer primaryKey ) 267 throws FinderException, EJBException
268 {
269 // find Employee in database 270 try {
271
272 // create SELECT statement
273 String select = "SELECT employeeID FROM Employee " + 274 "WHERE employeeID = ?";
275
276 // create PreparedStatement for SELECT 277 PreparedStatement selectStatement =
278 connection.prepareStatement( select );
279
280 // set employeeID value in PreparedStatement
281 selectStatement.setInt( 1, primaryKey.intValue() );
282
283 // execute selectStatement
284 ResultSet resultSet = selectStatement.executeQuery();
285
286 // return primary key if SELECT returns a record 287 if ( resultSet.next() ) {
288
289 // close resultSet and selectStatement 290 resultSet.close();
291 selectStatement.close();
292
293 return primaryKey;
294 } 295
296 // throw ObjectNotFoundException if SELECT produces 297 // no records
298 else
When a client invokes interface EmployeeHome method findByPrimaryKey, the EJB container invokes method
ejbFindByPrimaryKey
Obtain record from database via primary key
and SELECT statement
2002 Prentice Hall.
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 10).
Line 309
Lines 317-322
300 } 301
302 // throw EJBException if SELECT fails 303 catch ( SQLException sqlException ) {
304 throw new EJBException( sqlException );
305 }
306 } // end method ejbFindByPrimaryKey 307
308 // set EntityContext and create DataSource Connection 309 public void setEntityContext( EntityContext context ) 310 throws EJBException
311 {
312 // set entityContext 313 entityContext = context;
314
315 // look up the Employee DataSource and create Connection 316 try {
317 InitialContext initialContext = new InitialContext();
318
319 // get DataSource reference from JNDI directory 320 DataSource dataSource = ( DataSource )
321 initialContext.lookup(
322 "java:comp/env/jdbc/Employee" );
323
324 // get Connection from DataSource
325 connection = dataSource.getConnection();
326 } 327
328 // handle exception if DataSource not found in directory 329 catch ( NamingException namingException ) {
330 throw new EJBException( namingException );
331 } 332
EJB container invokes method set- EntityContext after the EJB is first created, but before the EJB is associated
with a particular database record
Use JNDI name and InitialContext to
locate Employee database in JNDI directory
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 11).
Line 340
Lines 361-364
333 // handle exception when getting Connection to DataSource 334 catch ( SQLException sqlException ) {
335 throw new EJBException( sqlException );
336 }
337 } // end method setEntityContext 338
339 // unset EntityContext
340 public void unsetEntityContext() throws EJBException 341 {
342 entityContext = null;
343
344 // close DataSource Connection 345 try {
346 connection.close();
347 } 348
349 // throw EJBException if closing Connection fails 350 catch ( SQLException sqlException ) {
351 throw new EJBException( sqlException );
352 } 353
354 // prepare connection for reuse 355 finally {
356 connection = null;
357 } 358 } 359
360 // set employeeID to null when container passivates EJB 361 public void ejbPassivate()
362 {
363 employeeID = null;
364 } 365
EJB container invokes method unsetEntityContext when
EJB is no longer needed
EJB container invokes method ejbPassivate to place active
EJB back in inactive pool
2002 Prentice Hall.
Outline
Fig. 15.3 EmployeeEJB implementation of Employee remote interface using bean- managed persistence (part 12).
Lines 367-370
366 // get primary key value when container activates EJB 367 public void ejbActivate()
368 {
369 employeeID = ( Integer ) entityContext.getPrimaryKey();
370 } 371 }
EJB container invokes method ejbActivate to activate
EJB from inactive pool
15.5.2 Employee EJB Deployment
• Deploying entity EJBs
– Similar to deploying session EJBs
15.5.2 Employee EJB Deployment (cont.)
Fig. 15.4 General dialog of New Enterprise Bean Wizard.
15.5.2 Employee EJB Deployment (cont.)
Fig. 15.5 Bean-Managed Persistence selected in Entity Settings dialog.
15.5.2 Employee EJB Deployment (cont.)
Fig. 15.6 Resource References dialog in New Enterprise Bean Wizard.
15.6 Employee EJB with Container- Managed Persistence
• Employee EJB
– Use container-managed persistence
• Simplify EJB implementation
2002 Prentice Hall.
Outline
Fig. 15.7 EmployeeEJB implementation of Employee remote interface using
container-managed persistence (part 1).
Lines 17-22 Lines 25-34
1 // EmployeeEJB.java
2 // EmployeeEJB is an entity EJB that uses container-managed 3 // persistence to persist Employee data in a database.
4 package com.deitel.advjhtp1.ejb.entity.cmp;
5
6 // Java core libraries
7 import java.rmi.RemoteException;
8
9 // Java standard extensions 10 import javax.ejb.*;
11
12 public class EmployeeEJB implements EntityBean { 13
14 private EntityContext entityContext;
15
16 // container-managed fields 17 public Integer employeeID;
18 public String socialSecurityNumber;
19 public String firstName;
20 public String lastName;
21 public String title;
22 public Double salary;
23
24 // get Employee ID
25 public Integer getEmployeeID() 26 {
27 return employeeID;
28 } 29
30 // set social security number
31 public void setSocialSecurityNumber( String number ) 32 {
33 socialSecurityNumber = number;
34 } 35
Declare container-managed fields (variables that container- managed persistence EJB uses to
store data) for Employee EJB.
Implement Employee remote interface
Outline
Fig. 15.7 EmployeeEJB implementation of Employee remote interface using
container-managed persistence (part 2).
Lines 37-70
36 // get social security number
37 public String getSocialSecurityNumber() 38 {
39 return socialSecurityNumber;
40 } 41
42 // set first name
43 public void setFirstName( String name ) 44 {
45 firstName = name;
46 } 47
48 // get first name
49 public String getFirstName() 50 {
51 return firstName;
52 } 53
54 // set last name
55 public void setLastName( String name ) 56 {
57 lastName = name;
58 } 59
60 // get last name
61 public String getLastName() 62 {
63 return lastName;
64 } 65
66 // set title
67 public void setTitle( String jobTitle ) 68 {
69 title = jobTitle;
Implement Employee remote interface
2002 Prentice Hall.
Outline
Fig. 15.7 EmployeeEJB implementation of Employee remote interface using
container-managed persistence (part 3).
Lines 73-88 Lines 91-96 Lines 102-105
71
72 // get title
73 public String getTitle() 74 {
75 return title;
76 } 77
78 // set salary
79 public void setSalary( Double amount ) 80 {
81 salary = amount;
82 } 83
84 // get salary
85 public Double getSalary() 86 {
87 return salary;
88 } 89
90 // create Employee instance
91 public Integer ejbCreate( Integer primaryKey ) 92 {
93 employeeID = primaryKey;
94
95 return null;
96 } 97
98 // do post-creation tasks when creating Employee 99 public void ejbPostCreate( Integer primaryKey ) {}
100
101 // set EntityContext
102 public void setEntityContext( EntityContext context ) 103 {
104 entityContext = context;
105 }
Implement Employee remote interface
Manage EntityContext member variable
EJB container executes SQL INSERT statement (supplied by deployer) after
method ejbCreate completes
Outline
Fig. 15.7 EmployeeEJB implementation of Employee remote interface using
container-managed persistence (part 4).
Lines 108-111 Lines 114-123
106
107 // unset EntityContext
108 public void unsetEntityContext() 109 {
110 entityContext = null;
111 } 112
113 // activate Employee instance 114 public void ejbActivate() 115 {
116 employeeID = ( Integer ) entityContext.getPrimaryKey();
117 } 118
119 // passivate Employee instance 120 public void ejbPassivate() 121 {
122 employeeID = null;
123 } 124
125 // load Employee instance in database 126 public void ejbLoad() {}
127
128 // store Employee instance in database 129 public void ejbStore() {}
130
131 // remove Employee instance from database 132 public void ejbRemove() {}
133 }
Manage EntityContext member variable
Perform same functions as those in bean-managed
persistence version
15.6 Employee EJB with Container- Managed Persistence (cont.)
Fig. 15.8 Container-Managed Persistence selected in Entity Settings dialog.
15.7 Employee EJB Client
• Class EmployeeEJBClient
– Interacts with entity EJB Employee – GUI provides JButtons:
• Finding Employee EJBs
• Adding Employee EJBs
• Updating Employee EJBs
• Deleting Employee EJBs
– GUI provides JTextFields:
• Displaying Employee information
2002 Prentice Hall.
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 1).
Line 22
1 // EmployeeEJBClient.java
2 // EmployeeEJBClient is a user interface for interacting with 3 // bean- and container-managed persistence Employee EJBs.
4 package com.deitel.advjhtp1.ejb.entity.client;
5
6 // Java core libraries 7 import java.awt.*;
8 import java.awt.event.*;
9 import java.text.*;
10 import java.util.*;
11 import java.rmi.RemoteException;
12
13 // Java standard extensions 14 import javax.swing.*;
15 import javax.ejb.*;
16 import javax.naming.*;
17 import javax.rmi.*;
18
19 // Deitel libraries
20 import com.deitel.advjhtp1.ejb.entity.*;
21
22 public class EmployeeEJBClient extends JFrame { 23
24 // variables for accessing EJBs
25 private InitialContext initialContext;
26 private EmployeeHome employeeHome;
27 private Employee currentEmployee;
28
29 // JTextFields for user input
30 private JTextField employeeIDTextField;
31 private JTextField socialSecurityTextField;
32 private JTextField firstNameTextField;
33 private JTextField lastNameTextField;
34 private JTextField titleTextField;
35 private JTextField salaryTextField;
GUI for finding, adding, updating and removing
Employee EJBs
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 2).
Lines 47-56
36
37 // BMPEmployeeEJBClient constructor
38 public EmployeeEJBClient( String JNDIName ) 39 {
40 super( "Employee EJB Client" );
41
42 // create user interface 43 createGUI();
44
45 // get EmployeeHome reference for Employee EJB 46 try {
47 initialContext = new InitialContext();
48
49 // look up Employee EJB using given JNDI name 50 Object homeObject =
51 initialContext.lookup( JNDIName );
52
53 // get EmployeeHome interface 54 employeeHome = ( EmployeeHome ) 55 PortableRemoteObject.narrow(
56 homeObject, EmployeeHome.class );
57 } 58
59 // handle exception when looking up Employee EJB 60 catch ( NamingException namingException ) {
61 namingException.printStackTrace( System.err );
62 } 63
64 // close application when user closes window 65 setDefaultCloseOperation( EXIT_ON_CLOSE );
66
67 // set size of application window and make it visible 68 setSize( 600, 300 );
69 setVisible( true );
Use InitialContext to look up Employee EJB using JNDIName argument
2002 Prentice Hall.
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 3).
Lines 76-77 Lines 99-100
71 } // end EmployeeEJBClient constructor 72
73 // prompt user for employeeID 74 private Integer getEmployeeID() 75 {
76 String primaryKeyString = JOptionPane.showInputDialog(
77 this, "Please enter an employeeID" );
78
79 // check if primaryKeyString is null, else return 80 // Integer
81 if ( primaryKeyString == null ) 82 return null;
83 else
84 return new Integer( primaryKeyString );
85 } 86
87 // get Employee reference for user-supplied employeeID 88 private void getEmployee()
89 {
90 // prompt user for employeeID and get Employee reference 91 Integer employeeID = getEmployeeID();
92
93 // return if employeeID is null 94 if ( employeeID == null )
95 return;
96
97 try {
98 // find Employee with given employeeID 99 Employee employee =
100 employeeHome.findByPrimaryKey( employeeID );
101
102 // update display with current Employee 103 setCurrentEmployee( employee );
104 } 105
Display JOptionPane to prompt user for employeeID
Use EmployeeHome method findByPrimaryKey to locate Employee EJB for Employee
with given employeeID
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 4).
Line 133
106 // handle exception when finding Employee 107 catch ( RemoteException remoteException ) { 108 JOptionPane.showMessageDialog(
109 EmployeeEJBClient.this,
110 remoteException.getMessage() );
111 } 112
113 // handle exception when finding Employee 114 catch ( FinderException finderException ) { 115 JOptionPane.showMessageDialog(
116 EmployeeEJBClient.this, "Employee not " + 117 "found: " + finderException.getMessage() );
118 }
119 } // end method getEmployee 120
121 // add new Employee by creating Employee EJB instance 122 private void addEmployee()
123 {
124 // prompt user for employeeID and create Employee 125 try {
126 Integer employeeID = getEmployeeID();
127
128 // return if employeeID null 129 if ( employeeID == null ) 130 return;
131
132 // create new Employee
133 Employee employee = employeeHome.create( employeeID );
134
135 // update display with new Employee 136 setCurrentEmployee( employee );
137 } 138
Use EmployeeHome method create to create Employee EJB for Employee with given employeeID
2002 Prentice Hall.
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 5).
Lines 159-171
139 // handle exception when creating Employee 140 catch ( CreateException createException ) { 141 JOptionPane.showMessageDialog( this, 142 createException.getMessage() );
143 } 144
145 // handle exception when creating Employee 146 catch ( RemoteException remoteException ) { 147 JOptionPane.showMessageDialog( this, 148 remoteException.getMessage() );
149 }
150 } // end method addEmployee 151
152 // update current Employee with user-supplied information 153 private void updateEmployee()
154 {
155 // get information from JTextFields and update Employee 156 try {
157
158 // set Employee socialSecurityNumber
159 currentEmployee.setSocialSecurityNumber(
160 socialSecurityTextField.getText() );
161
162 // set Employee firstName
163 currentEmployee.setFirstName(
164 firstNameTextField.getText() );
165
166 // set Employee lastName
167 currentEmployee.setLastName(
168 lastNameTextField.getText() );
169
170 // set Employee title
171 currentEmployee.setTitle( titleTextField.getText() );
172
Use values provided in
JTextFields to update current Employee’s information
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 6).
Lines 187-191
173 // set Employee salary
174 Double salary = new Double( salaryTextField.getText() );
175 currentEmployee.setSalary( salary );
176
177 } // end try 178
179 // handle exception invoking Employee business methods 180 catch ( RemoteException remoteException ) {
181 JOptionPane.showMessageDialog( this, 182 remoteException.getMessage() );
183 }
184 } // end method updateEmployee 185
186 // delete current Employee 187 private void deleteEmployee() 188 {
189 // remove current Employee EJB 190 try {
191 currentEmployee.remove();
192
193 // clear JTextFields
194 employeeIDTextField.setText( "" );
195 socialSecurityTextField.setText( "" );
196 firstNameTextField.setText( "" );
197 lastNameTextField.setText( "" );
198 titleTextField.setText( "" );
199 salaryTextField.setText( "" );
200
201 } // end try 202
203 // handle exception when removing Employee 204 catch ( RemoteException remoteException ) { 205 JOptionPane.showMessageDialog( this, 206 remoteException.getMessage() );
Delete current Employee from data by invoking Employee EJB
method remove
2002 Prentice Hall.
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 7).
Line 217
Lines 224-240
208
209 // handle exception when removing Employee 210 catch ( RemoveException removeException ) { 211 JOptionPane.showMessageDialog( this, 212 removeException.getMessage() );
213 }
214 } // end method deleteEmployee 215
216 // update display with current Employee information 217 private void setCurrentEmployee( Employee employee ) 218 {
219 // get information for currentEmployee and update display 220 try {
221 currentEmployee = employee;
222
223 // get the employeeID
224 Integer employeeID = ( Integer ) 225 currentEmployee.getEmployeeID();
226
227 // update display
228 employeeIDTextField.setText( employeeID.toString() );
229
230 // set socialSecurityNumber in display 231 socialSecurityTextField.setText(
232 currentEmployee.getSocialSecurityNumber() );
233
234 // set firstName in display 235 firstNameTextField.setText(
236 currentEmployee.getFirstName() );
237
238 // set lastName in display 239 lastNameTextField.setText(
240 currentEmployee.getLastName() );
241
Utility method to update GUI information from Employee EJB
Retrieve values for each Employee property and update
JTextFields
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 8).
Lines 243-258 Line 269
242 // set title in display
243 titleTextField.setText( currentEmployee.getTitle() );
244
245 // get Employee salary
246 Double salary = (Double) currentEmployee.getSalary();
247
248 // ensure salary is not null and update display 249 if ( salary != null ) {
250 NumberFormat dollarFormatter =
251 NumberFormat.getCurrencyInstance(
252 Locale.US );
253
254 salaryTextField.setText( dollarFormatter.format(
255 salary ) );
256 } 257 else
258 salaryTextField.setText( "" );
259 } // end try 260
261 // handle exception invoking Employee business methods 262 catch ( RemoteException remoteException ) {
263 JOptionPane.showMessageDialog( this, 264 remoteException.getMessage() );
265 }
266 } // end method setCurrentEmployee 267
268 // create the application GUI 269 private void createGUI()
270 {
271 // create JPanel for Employee form components
272 JPanel formPanel = new JPanel( new GridLayout( 6, 2 ) );
273
Retrieve values for each Employee property and update
JTextFields
Build user interface for EmployeeEJBClient
2002 Prentice Hall.
Outline
Fig. 15.9
EmployeeEJB- Client for interacting with Employee EJB (part 9).
Lines 275-303
274 // create read-only JTextField for employeeID 275 employeeIDTextField = new JTextField();
276 employeeIDTextField.setEditable( false );
277 formPanel.add( new JLabel( "Employee ID" ) );
278 formPanel.add( employeeIDTextField );
279
280 // create JTextField and JLabel for social security # 281 socialSecurityTextField = new JTextField();
282 formPanel.add( new JLabel( "Social Security #" ) );
283 formPanel.add( socialSecurityTextField );
284
285 // create JTextField and JLabel for first name 286 firstNameTextField = new JTextField();
287 formPanel.add( new JLabel( "First Name" ) );
288 formPanel.add( firstNameTextField );
289
290 // create JTextField and JLabel for last name 291 lastNameTextField = new JTextField();
292 formPanel.add( new JLabel( "Last Name" ) );
293 formPanel.add( lastNameTextField );
294
295 // create JTextField and JLabel for job title 296 titleTextField = new JTextField();
297 formPanel.add( new JLabel( "Title" ) );
298 formPanel.add( titleTextField );
299
300 // create JTextField and JLabel for salary 301 salaryTextField = new JTextField();
302 formPanel.add( new JLabel( "Salary" ) );
303 formPanel.add( salaryTextField );
304
305 // add formPanel to the JFrame's contentPane 306 Container contentPane = getContentPane();
307 contentPane.add( formPanel, BorderLayout.CENTER );
308
Create JTextFields and JLabels for each
Employee