Professional Documents
Culture Documents
private static void count Teacher s( boolean onl yRel i ef ) {
try {
Pr epar edSt at ement pst mt = ( onl yRel i ef ) ?
pst mt Count Rel i ef Teacher s : pst mt Count Teacher s;
Resul t Set r s = pst mt . execut eQuer y( ) ;
if ( r s. next ( ) ) {
int count = r s. get I nt ( 1) ;
if ( onl yRel i ef ) {
Syst em. out . pr i nt l n( " Ther e ar e " + count +
" r el i ef t eacher s" ) ;
} else {
Syst em. out . pr i nt l n( " The depar t ment empl oys " + count +
" t eacher s" ) ;
}
} else {
Syst em. out . pr i nt l n( " Fai l ed t o r et r i eve t eacher dat a" ) ;
}
r s. cl ose( ) ;
} catch ( SQLExcept i on ex) { }
}
The program does make some use of simple POJ O (Plain Old J ava Object) classes to
hold the data for records of Teachers, Pupils etc.
Programmatically
construct beans
to hold retrieved
data
private static Teacher get Teacher ( Long i d) {
Teacher t = null;
// Attempt to retrieve Teacher record given employment
// number
try {
pst mt Speci f i cTeacher . set Long( 1, i d) ;
Resul t Set r s = pst mt Speci f i cTeacher . execut eQuer y( ) ;
if ( r s. next ( ) ) {
t = new Teacher (
r s. get St r i ng( " EMPLOYEENUMBER" ) ,
r s. get St r i ng( " TI TLE" ) ,
r s. get St r i ng( " SURNAME" ) ,
r s. get St r i ng( " FI RSTNAME" ) ,
r s. get St r i ng( " I NI TI ALS" ) ,
r s. get St r i ng( " SCHOOLNAME" ) ,
8
nabg, 2008
Starting J PA: Old fashioned J DBC style
r s. get St r i ng( " ROLE" ) ) ;
}
r s. cl ose( ) ;
} catch ( SQLExcept i on ex) { }
return t ;
}
Often, simple CRUD programs implemented using J DBC will forgo entity classes
and work simply with the raw data associated with SQL results and queries. Though
common, such a programming style further reduces the intelligibility of the
programs:
private static final St r i ng get Teacher sAt School =
" sel ect * f r omTeacher wher e school name=?" ;
private static Pr epar edSt at ement pst mt Teacher sFor School ;
private static void l i st Teacher sBySchool ( St r i ng school Name) {
try {
pst mt Teacher sFor School . set St r i ng( 1, school Name) ;
Resul t Set r s = pst mt Teacher sFor School . execut eQuer y( ) ;
int count = 0;
while ( r s. next ( ) ) {
if ( count == 0)
Syst em. out . pr i nt l n( " Teacher s empl oyed at " +
school Name) ;
St r i ng empNum= r s. get St r i ng( 1) ;
St r i ng t i t l e = r s. get St r i ng( 5) ;
St r i ng sur name = r s. get St r i ng( 2) ;
St r i ng f i r st name = r s. get St r i ng( 3) ;
St r i ng i ni t s = r s. get St r i ng( 4) ;
if( i ni t s==null) i ni t s =" " ;
St r i ng l vl = r s. get St r i ng( 7) ;
if( l vl ==null) l vl = " - " ;
Syst em. out . pr i nt l n( " \ t " +
empNum+" : " +
t i t l e + " " +
f i r st name + " " +
i ni t s + " " +
sur name + " " +
l vl
) ;
count ++;
}
if ( count == 0) {
Syst em. out . pr i nt l n(
Try to manage
without beans
the code is even
worse!
" Ther e ar e no t eacher s cur r ent l y empl oyed at " +
schoolName);
}
rs.close();
} catch (SQLException ex) { }
}
Use of simple bean classes
9
nabg, 2008
Starting J PA: Old fashioned J DBC style
Classes such as Teacher will just be beans a few private data members
corresponding to the columns in the relational table, and appropriate accessor and
mutator functions:
public class Teacher {
private St r i ng empl oyeeNumber ;
private St r i ng t i t l e;
private St r i ng f i r st Name;
private St r i ng i ni t i al s;
private St r i ng sur Name;
private St r i ng school Name;
private St r i ng r ol e;
public Teacher ( ) { }
public Teacher ( St r i ng anEmpNum, St r i ng at i t l e,
St r i ng asur Name, St r i ng af i r st Name, St r i ng i ni t s,
St r i ng sName, St r i ng r ol ename
)
{
empl oyeeNumber = anEmpNum;
t i t l e = at i t l e;
}
public St r i ng get Ti t l e( ) {
return t i t l e;
}
public void set Ti t l e( St r i ng t i t l e) {
this. t i t l e = t i t l e;
}
@Over r i de
public St r i ng t oSt r i ng( )
{
// Some effective definition of toString
Definition of a
simple bean class
}
}
Entity relationships
not clearly
expressed in bean
classes.
Note how a Teacher object references its associated School. It has a string for the
schoolname a primary key for the School table. If the application code needed to
print out details of where a teacher with specified employment number was
employed, it would have to first run one query to retrieve the teacher record and then
another query to load the School record. These classes are limited; they dont really
capture anything of the relationships that can exist between class instances. With
the J PA style, we will manage better.
Example create operations:
Other operations, such as those that create new records in the database, are equally
simple but tiresome as a consequence of their use of the low level J DBC API. Thus,
10
nabg, 2008
Starting J PA: Old fashioned J DBC style
the following procedure correctly updates the database but fails to convey the idea
of create a new Teacher object:
private static final St r i ng addTeacher =
" i nser t i nt o Teacher val ues( ?, ?, ?, ?, ?, nul l , nul l ) " ;
private static Pr epar edSt at ement pst mt AddTeacher ;
private static void addTeacher ( ) {
try {
St r i ng[ ] t i t l esAr r ay = {" Dr " , " Mr s" , " Ms" , " Mi ss" , " Mr " };
St r i ng empNumber = J Opt i onPane. showI nput Di al og(
" Empl oyment number " ) ;
Obj ect sel ect ed = J Opt i onPane. showI nput Di al og( null,
" Sel ect t i t l e" , " Per son\ ' s t i t l e" ,
J Opt i onPane. I NFORMATI ON_MESSAGE,
null, t i t l esAr r ay, t i t l esAr r ay[ 0] ) ;
if ( sel ect ed == null)
return;
St r i ng t i t l e = ( St r i ng) sel ect ed;
St r i ng f i r st Name = J Opt i onPane. showI nput Di al og(
" Fi r st name" ) ;
St r i ng i ni t i al s = J Opt i onPane. showI nput Di al og( " I ni t i al s" ) ;
St r i ng sur Name = J Opt i onPane. showI nput Di al og( " Sur ame" ) ;
pst mt AddTeacher . set St r i ng( 1, empNumber ) ;
pst mt AddTeacher . set St r i ng( 2, sur Name ) ;
pst mt AddTeacher . set St r i ng( 3, f i r st Name ) ;
pst mt AddTeacher . set St r i ng( 4, i ni t i al s) ;
pst mt AddTeacher . set St r i ng( 5, t i t l e ) ;
pst mt AddTeacher . execut eUpdat e( ) ;
} catch ( SQLExcept i on ex) { }
}
Inserting rows?
Hack, hack,
hack!
With J PA, the code for all such operations becomes much more intelligible.
JPA style
Entity relationships expressed in classes
J PA program outline
Defining the database connection via meta-data
The EntityManager an intelligent database connection
Simple J PA selection query
J PA Read operations
J PA Create and Update operations
Costs
J PA functions illustrated
Deployment
The application was recoded to work with J PA. J PA uses formally defined entity
classes. Though still just POJ O classes, these entity classes can be designed to
express the relations that exist among entities.
11
nabg, 2008
Starting J PA: New, cleaner, J PA style
Entity relationships expressed in classes:
In this example application, one-to-many relationships exist between a school and
both its staff and its students. These relationships can be defined by having a School
class whose data members include Collection<Teacher>and Collection<Pupil>.
Rather than have a Teacher object have a String with the name of the school were
that teacher is employed, the data member can be a reference to the appropriate
School object. When these relationships are captured in the class structure, the code
becomes more readily comprehended.
Entity
relationships
through object
references
Entity classes can be defined by the application developer and can be processed to
create the tables. But, here, the tables already existed in the database making it
easier to have the classes auto-generated. A J PA system can read table meta-data
and construct initial outlines for the entity classes. This was the first step taken in
the development of the J PA version of the application.
The definition generated (using the Oracle Toplink J PA implementation via
NetBeans) for Teacher is as follows:
@Ent i t y
@Tabl e( name = " TEACHER" )