Lab3: Entity Beans

minutetwitterSoftware and s/w Development

Jun 7, 2012 (5 years and 4 months ago)

317 views

Lab3: Entity Beans
Geoff Sharman, Jianing Wang
November 2009
Aim:
To develop an entity bean that is associated with a database table in a MySQL
server, and to retrieve student’s date of birth information via this entity bean in
the session bean we developed from Lab2. The retrieved information will be
displayed in a JSP page.
Introduction:
An
entity bean
represents a business object in a persistent storage mechanism.
In Java EE, the persistent storage mechanism is a relational database. Typically,
each entity bean has an underlying table in a relational database, and each
instance
of the bean corresponds to a
row
in that table.
An entity bean has some unique features different from session beans, e.g.,
persistent, allow shared access, have primary keys, and may participate in
relationships with other entity beans.
Persistence:
Because the state of an entity bean is saved in a storage mechanism, it is
persistent.
Persistence
means that the entity bean's state exists beyond the
lifetime of the application or the Java EE server process. The data in a database
is persistent because it still exists even after you shut down the database server
or the applications it services.
There are two types of persistence for entity beans:
bean-managed
and
container-managed
. With
bean-managed persistence
, the entity bean code that
you write contains the calls that access the database, which means SQL queries
need to be written by programmers explicitly. If your bean has container-
managed persistence, the EJB container
automatically
generates the necessary
database access calls. The code that you write for the entity bean does not
include these calls. In Lab3, we will use the latter method.
Shared Access
Entity beans may be shared by multiple clients. Because the clients might want
to change the same data, it's important that entity beans work within
transactions. Typically, the EJB container provides transaction management. In
1
Lab3, we only need to retrieve information from a database via an entity bean
and the database will not be modified.
Primary Key
Each entity bean has a unique object identifier. A customer entity bean, for
example, might be identified by a customer number. The unique identifier, or
primary key
, enables the client to locate a particular entity bean. In Lab3, we will
use student ID to identify a student and retrieve information accordingly.
Relationships
Like a table in a relational database, an entity bean may be related to other entity
beans. Relationships need to be implemented differently in entity beans with
bean-managed persistence and those with container-managed persistence. With
bean-managed
persistence, the code that you write has to implement the
relationships. But with
container-managed
persistence, the EJB container takes
care of the relationships for you. In Lab3, we will use only one relational table,
therefore, no additional relationships will be considered except Primary Key.
Instruction:
1.
Open the NetBeans IDE.
2.
Create an Entity Bean
a.
Right Click on the EJB module, and select “New”

“Entity Classes
from Database…”
2
b.
In the appeared window, choose “New Data Source…” for the “Data
Source:” tag. This step enables you to locate a data source, and
retrieve available tables from that database, and define corresponding
entity beans to these tables.
3
c.
In the appeared window, ignore the “JNDI Name” tag first, but we will
define it in the later step. Choose “New Database Connection…” in the
“Database Connection” tag.
d.
In the next window, select “
MySQL (connector/J driver)
” for the “Name”
tag, “
hermes.dcs.bbk.ac.uk
” for the “Host” tag, “
3306
” for the “Port”
tag, “
j2eedb
” for the “Database” tag, “
jianing
” for the “User Name” tag,
and “
jianing88
” for the “Password” tag. Also tick the “Remember
password” tickbox. Click “OK” to finish.
4
e.
Now back to the “Create Data Source” window, give a JNDI name.
This JNDI name will appear in the JNDI tree in the GlassFish server;
therefore, it has to be unique. Click “OK” when you finish.
f.
Back to the “New Entity Classes from Database” window, you can see
that “students” appear in the “Available Tables” field. In the “j2eedb”
database in MySQL server, “students” is a relational table, containing
three attributes
5
i.
sid

, which contains the student ID and the primary key for the
students table.
ii.
sname

, which contains the student names.
iii.
dob

, which contains student’s date of birth in the format of
year.month.date, e.g., 1980.1.30.
Select “students” and click “Add>” to move the students table from
“Available Tables” to “Selected Tables”. And click “Next”.
g.
Click “Create Persistence Unit…”. Persistence Unit will allow you to
keep the data type and structure to be persistent between your java
code and the database, and data persistent if you modify any content
of database.
6
h.
Leave all fields untouched and click “Create”. The “Persistent Unit
Name” will be used later when creating an Entity Manager that is used
to interact with data sources.
i.
Back to the “New Entity Classes from Database” window, Click
“Finish”.
7
j.
In the NetBeans IDE, a “Students.java” file is created in the EJB
module.
3.
Use Entity Beans in a Session Bean.
a.
Open the Session Bean you created in Lab2.
b.
Add the code below to create a private variable in your session bean
class. Also add the corresponding import classes (A hint will be
displayed in NetBeans indicating which class should be imported).
EntityManager is used to create and remove persistent entity
instances, to find entities by their primary key, and to query over
entities.
@PersistenceContext(unitName="MyEnterpriseLab-ejbPU")
private EntityManager em;
We need to modify the unitName in the above code. The
‘MyEnterpriseLab-ejbPU’

is the “Persistence Unit Name” in the
configuration file which will initial a persistent unit (See below). This file
is written in XML format and you can find it from ‘Configuration files’
folder in your EJB module in the project window. It specifies which
table in database your java code will be consistent with.
8
c.
Add the code below to the area where the input student ID is numeric
and within the range of 1 to 10. This code will get student’s dob
according to student’s ID, and get the current system time, calculate
the student’s age.
Note
: The variable name in the code may be different from the one
you use in your session bean. Modify them if necessary.
Students st = em.find(Students.class, input);
if (st != null) {
String dob = st.getDob();
returnVal = dob.substring(0,4);
Calendar cal = Calendar.getInstance();
int year = cal.get(cal.YEAR);
int age = year - Integer.valueOf(returnVal).intValue();
returnVal = new Integer(age).toString();

} else {
returnVal = "Cannot get record from database. Check the input studentID";
}
9
4.
Clean and Build the project, and Deploy it to the GlassFish server. Check the
JSP page you created in Lab2. Does it retrieve the correct information for the
Student ID you give?
Note
: Make sure the GlassFish server is running.
10