Hands on Lab - Building Java EE application using JDeveloper 12C

This tutorial shows you how to build Java EE application that interacts with a database using
JDeveloper. You'll be using Oracle JDeveloper 12C ersion 12.1.2.!.! and "# sche$a which
co$es with Oracle database.
1. %& you don't have JDeveloper 12C installed( download and install JDeveloper 12C. You
$ay &ind the installer here http)**www.oracle.co$*technetwor+*developer,
tools*-dev*downloads*inde..ht$l
2. %& you don't have any Oracle database available &or use( download and install Oracle /E
database. 0e will use "# sche$a &ro$ this database &or building this sa$ple. 1teps &or
installing Oracle /E here)
a. You can download Oracle /E &ro$ here
http)**www.oracle.co$*technetwor+*database*database,technologies*e.press,
edition*downloads*inde..ht$l
b. Your database should have the "# sche$a unloc+ed.
i. To do this( &ro$ a co$$and line issue the co$$and) sqlplus system
ii. 2rovide the password &or the syste$ user.
iii. %n the 1342451 pro$pt write the &ollowing co$$and Alter user HR
account unlock ;
iv. Alter user HR identified by HR;
6. 7e&ore starting building the application( let us set up essential environ$ent properties &or
the %DE which includes pro.y setting &or the %DE 8i& you are wor+ing behind corporate
&irewall9( build tool and source control. %n this lab we will &ocus only on $aven and 1:
as build tool and source control( respectively although JDeveloper supports $any other
tools.
;. You can s+ip pro.y settings &or JDeveloper i& you are not wor+ing behind a &irewall. To
set up the pro.y &or the JDeveloper( open JDeveloper and navigate to ($ain $enu(
choose Tools < 2re&erences. %n the 2re&erences dialog select 0eb 7rowser and 2ro.y
and then select 2ro.y 1ettings tab. 1peci&y the pro.y as appropriate.
=. JDeveloper 12C co$es with built in $aven located at
>JDev?"o$e<@oracle?co$$on@$odules@org.apache.$aven?6.!.;.
A. You can s+ip the below steps and proceed to 1tep B i& you don't pre&er to use your local
$aven installation &ro$ JDeveloper. %& you want to use a e.ternal $aven installation
8that you $ay have already set up in your $achine9 &ro$ JDeveloper %DE( then do the
&ollowing)
a. %n JDeveloper( navigate to $ain $enu( choose Tools < 2re&erence. %n the
2re&erences dialog choose Caven and clic+ on 4oad E.tension on right pane8this
needs to be done only i& this is the &irst ti$e you are accessing this option9
b. %n the 2re&erences dialog( choose the e.ternal version and set the path to your
local $aven ho$e. Do not close the dialog nowD we do have &ew $ore settings to
add.
c. To set up a custo$ local repository &or $aven tool when used in the %DE( choose
#epositories displayed below the Caven node on le&t pane in the 2re&erence
dialog window and then set the local repository path on right hand side panel as
appropriate. Even you can speci&y di&&erent central repositories using this screen.
d. To use custo$ settings..$l &or $aven tool when you build &ro$ %DE( choose
1ettings displayed below the Caven node on le&t pane in the 2re&erence dialog
and set the path to your settings..$l. Optionally set additional setting using
Edditional 1ettings tab. Clic+ OF to dis$iss the dialog.
B. You are done with all basic setups. :ow( let us start building a si$ple Java EE
application. %n the $ain $enu( choose Gile < :ew<Epplication
H. %n the :ew Iallery choose Java EE 0eb application. %n the 1tep 1 o& the dialog( enter
application na$e as JavaEEDemo and set a directory &or storing the application source.
Clic+ :e.t.
J. %n 1tep 2 o& the dialog( you can speci&y Codel pro-ect na$e and directory. Gor this de$o(
accept the de&ault values populated by the %DE as is. Clic+ :e.t.
1!. %n 1tep 6 o& the dialog( you can speci&y pac+age &or the Codel pro-ect and Output
directory. Gor this de$o( leave the de&ault values populated by the %DE as is and clic+
:e.t.
11. %n 1tep ; o& the dialog( you can con&igure EJ7. Ca+e sure you have the selected the
&ollowing settings. Clic+ :e.t.

12. %n 1tep = o& the dialog( you can speci&y the 5% pro-ect na$e and source directory. Gor
this de$o( leave the de&ault values populated by the %DE as is and clic+ :e.t.
16. %n 1tep A o& the dialog( you can speci&y the pac+age na$e &or 5% pro-ect na$e and
source path. Gor this de$o( leave the de&ault values populated by the %DE and clic+
:e.t.
1;. %n 1tep B( you can speci&y the build environ$ent. Choose the Caven option in the dialog
and leave the de&ault values populated by the %DE without any change. Clic+ Ginish.
The generated wor+space $ay now loo+ li+e as shown below.
The basic structure is in place now( with two pro-ects) Codel and iew Controller. Codel
is supposed to hold business service i$ple$entation and iewController holds 5% pages
and 5% centric logic.
1=. You $ay see po$..$l &ile generated under each pro-ect. 7y de&ault dependency entries
in these po$ &iles are in synch with dependencies that you set &or a pro-ect using
dependency editor window o&&ered by the %DE. To view the editor option &or setting
dependency( right clic+ on a pro-ect( choose 2ro-ect 2roperties < choose 4ibraries and
Classpath. Do not add anything &or this lab. Clic+ OF to dis$iss this dialog.
1A. To turn o&& the auto$ated synchroniKation o& dependency entries between po$..$l and
%DE editor 8that you have seen in last step9( you can do the &ollowing.
a. #ight clic+ the pro-ect &or which you want to turn o&& the auto, synchroniKation and
then choose 2ro-ect 2roperties
b. %n the 2ro-ect 2roperties dialog( select Caven and re$ove the 2ro-ect 2OC &ile
entry. 2lease do not re$ove any entry this &or this lab. Clic+ OF to dis$iss the
dialog.
1B. :e.t is build the logic &or reading and storing data to database using J2E and EJ7. #ight
clic+ the Codel pro-ect( choose :ew < Entities &ro$ Tables.
1H. %n 1tep 2 o& Create Entities &ro$ Tables( accept the de&ault na$e populated by the %DE
&or the 2ersistence 5nit and clic+ :e.t.
1J. %n 1tep 6 o& Create Entities &ro$ Tables( choose Online Database Connection and clic+
:e.t.
2!. %n 1tep ; o& Create Entities &ro$ Tables( clic+ green plus icon to create a D7 connection.
%n the Create Database Connection dialog( enter connection na$e( user na$e(
password and JD7C settings. This e.a$ple uses "# sche$a( so entries should be
based on the database that you are using.
Clic+ OF to close the Create Database Connection dialog and then clic+ :e.t on Create
Entities &ro$ Tables wiKard.
21. %n 1tep =( clic+ 3uery button to display the tables &ro$ the database sche$a. Gor this
e.a$ple( select DE2E#TCE:T1 and EC24OYEE1 and shuttle the$ to right. Clic+
:e.t
22. %n 1tep A( you can set the co$$on properties &or the entities that are being generated in
the ne.t steps. Gor this de$o application( you can accept the de&ault values set by %DE
and clic+ :e.t.
26. %n 1tep B( speci&y the entity details that are generated &or the Table that you selected in
1tep = o& this dialog window. Eccept the de&ault settings and Clic+ :e.t.
2;. %n 1tep H( %DE reads data dictionary &or the selected tables and o&&ers associations
based on the &oreign +ey constraints &or the tables. Eccept the de&ault settings and clic+
:e.t.
2=. Clic+ Ginish to generate entities and related $etadata. Your Codel pro-ect $ay loo+ li+e
as shown in the &ollowing screen shot.
2A. :e.t is to build a stateless session bean e.posing operations on entities &or use by the
client. To do this( right clic+ the Codel pro-ect( choose :ew < 1ession 7ean. You $ay
see Create 1ession 7ean wiKard dialog now which $ay help to generate stateless
session bean e&&ortlessly.
2B. %n 1tep 2 o& Create 1ession 7ean wiKard( set the general properties such as na$e(
session type( entity type( persistence unit etc. Gor this de$o application( accept the
de&ault values populated by the %DE and clic+ :e.t.
2H. %n 1tep 6( you can select the de&ault $ethods o&&ered by the wiKard &or the session bean.
1elect all the $ethods and clic+ :e.t.
2J. %n 1tep ;( speci&y the bean class na$e and directory. Eccept the de&ault values and clic+
:e.t.
6!. %n 1tep =( deselect #e$ote %nter&ace( we do not need it. Chec+ L%$ple$ent 4ocal
%nter&aceM option and clic+ :e.t.
61. Clic+ Ginish. %DE then generates session bean based on the settings.
62. The basic business service i$ple$entation is ready &or use now. :ow let us enable
CD%8Conte.ts and Dependency %n-ection9 &or this pro-ect so that client can consu$e the
session bean that we created using CD%.
#ight clic+ the CETE,%:G &older in the $odel pro-ect( choose :ew < Gro$ Iallery <
Ieneral N /C4 < /C4 Docu$ent. 1et the na$e as beans..$l. Copy and paste the below
contents to this .$l &ile.
>O.$l version P '1.!' encoding P 'windows,12=2'O<
>beans .$lns).siPQhttp)**www.w6.org*2!!1*/C41che$a,instanceQ
.si)sche$a4ocationPQhttp)**-ava.sun.co$*.$l*ns*-avaee
http)**-ava.sun.co$*.$l*ns*-avaee*beans?1?!..sdQ
.$lnsPQhttp)**-ava.sun.co$*.$l*ns*-avaeeQ<>*beans<
Note that New Gallery window has option to generate the beans.xml visually. To do this,
in the New Gallery window choose Context and Dependency Injection beans.xml
option, which will help you to generate the beans.xml. !e are not using that option here
because ID" adds the xml to !"#$IN% &older 'irrespective o& the project type( which
may not wor) &or jar archive, rather meant &or the web projects 'war archive(. It is a bug
in *+C.
You can add $ore CD% &eatures visually using the JDeveloper %DE. To use this %DE
support( you $ust right clic+ the pro-ect and then choose :ew < Gro$ Iallery <
Conte.ts and Dependency %n-ection. You can choose the option &ro$ the right side pane
as appropriate. 0e are not going to discuss these options as they are purely Java EE
CD% &eatures and %DE -ust $a+es your li&e easier by generating the appropriate classes.
66. 0e are done with $odel pro-ect &or this de$o application. 4et us build the 5% now. Es we
are going to use CD% to grab session bean &ro$ client( let us enable CD% &or this pro-ect
as &irst step. #ight clic+ the iewController pro-ect( and choose :ew < Gro$ Iallery <
Conte.ts and Dependency %n-ection < beans..$l. This action adds beans..$l to the
0E7,%:G &older.
6;. 4et us de&ine two J1G pages to display depart$ent and e$ployee data and de&ined
navigation rules between the$. %n the iew Controller pro-ect( double clic+ the &aces,
con&ig..$l &ile displayed under 0eb Content &older. Drag and drop the J1G page &ro$
co$ponents palette 8displayed on right hand side o& the %DE9 to &aces,con&ig..$l editor
and na$e it as dept.-s&. Double clic+ the dept.-s& in the &aces,con&ig..$l editor to
generate the physical &ile.
1witch bac+ to &aces,con&ig..$l editor and drop another J1G 2age and na$e it as
e$p.-s&. Double clic+ the e$p.-s& in the &aces,con&ig..$l editor to generate the physical
&ile.
6=. 1witch bac+ to the &aces,con&ig..$l editor( draw navigation between dept.-s& and e$p.-s&
by choosing J1G :avigation option &ro$ co$ponents palette. 4abel the navigation case
as viewE$p as shown below. This is stored in &aces,con&ig..$l as navigation,case.
6A. De&ine a CD% bean in the view controller pro-ect to access the session bean de&ined in
the $odel pro-ect. This bean will be holding client side logic also code &or accessing
session bean that we created. To do this( right clic+ the iewController pro-ect( choose
:ew < Gro$ Iallery < Java< Class. %n the Create Java Class dialog( speci&y the na$e
o& the class as Dept7ean and accept the de&ault values &or other &ields and clic+ OF.
This bean will act as client side abstraction 8pro.y9 &or the service layer. Edd
R1ession1coped annotation to $ar+ it as CD% session scoped bean and a $ethod to
return depart$ent collection &or use &ro$ the dept.-s&. You can copy and paste the
&ollowing code to save ti$e.
pac+age viewD
i$port -ava.io.1erialiKableD
i$port -ava..e-b.EJ7D
i$port -ava..enterprise.conte.t.1ession1copedD
i$port -ava..&aces.$odel.DataCodelD
i$port -ava..&aces.$odel.4istDataCodelD
i$port -ava..in-ect.:a$edD
i$port $odel.1essionEJ77eanD
i$port $odel.1essionEJ74ocalD
R1ession1coped
R:a$ed8Qdept7eanQ9
public class Dept7ean i$ple$ents 1erialiKable S
REJ7
private 1essionEJ74ocal sessionEJ77eanD
private DataCodel depts P nullD
public Dept7ean89 S
super89D
T
public DataCodel getDepart$ents89 S
i& 8depts PP null9 S
depts P new 4istDataCodel8sessionEJ77ean.getDepart$entsGindEll899D
T
return deptsD
T
T
1i$ilarly( de&ine E$p7ean.Java CD% bean in the iewController pro-ect &or returning
e$ployee collection. 1ource code &or E$p7ean.Java is shown below.
pac+age viewD
i$port -ava.io.1erialiKableD
i$port -ava..e-b.EJ7D
i$port -ava..enterprise.conte.t.1ession1copedD
i$port -ava..&aces.$odel.DataCodelD
i$port -ava..&aces.$odel.4istDataCodelD
i$port -ava..in-ect.:a$edD
i$port $odel.1essionEJ77eanD
i$port $odel.1essionEJ74ocalD
R1ession1coped
R:a$ed8Qe$p7eanQ9
public class E$p7ean i$ple$ents 1erialiKable S
REJ7
private 1essionEJ74ocal sessionEJ77eanD
private DataCodel e$ps P nullD
public E$p7ean89 S
super89D
T
public DataCodel getE$ployees89 S
i& 8e$ps PP null9 S
e$ps P new 4istDataCodel8sessionEJ77ean.getE$ployeesGindEll899D
T
return e$psD
T
T
6B. Open the dept.-s& that we built a while ago( and add a table to display depart$ent data
returned by Dept7ean. Edd a button to navigate to e$p.-s& as well. The page source
$ay loo+ li+e as given below.
>O.$l versionP'1.!' encodingP'windows,12=2'O<
>UDOCTY2E ht$l<
>&)view .$lns)&PQhttp)**-ava.sun.co$*-s&*coreQ .$lns)hPQhttp)**-ava.sun.co$*-s&*ht$lQ<
>ht$l .$lnsPQhttp)**www.w6.org*1JJJ*.ht$lQ<
>h)head<>*h)head<
>h)body<
>h)&or$<
>h)panelIroup<
>h)dataTable valuePQVSdept7ean.depart$entsTQ varPQite$Q borderPQ!Q
cellpaddingPQ2Q cellspacingPQ!Q
rulesPQallQ stylePQborder)solid 1p.Q<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQDepart$ent %dQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.depart$ent%dTQ*<
>*h)colu$n<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQDepart$ent :a$eQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.depart$ent:a$eTQ*<
>*h)colu$n<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQCanager %dQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.e$ployees1.e$ployee%dTQ*<
>*h)colu$n<
>*h)dataTable<
>h)co$$and7utton valuePQiew E$ployeesQ actionPQviewE$pQ*<
>*h)panelIroup<
>*h)&or$<
>*h)body<
>*ht$l<
>*&)view<
1i$ilarly open e$p.-s& and add code &or displaying e$ployee's data. 1ource is given
below &or a Wuic+ re&erence.
>O.$l versionP'1.!' encodingP'windows,12=2'O<
>UDOCTY2E ht$l<
>&)view .$lns)&PQhttp)**-ava.sun.co$*-s&*coreQ .$lns)hPQhttp)**-ava.sun.co$*-s&*ht$lQ<
>ht$l .$lnsPQhttp)**www.w6.org*1JJJ*.ht$lQ<
>h)head<>*h)head<
>h)body<
>h)&or$<
>h)panelIroup<
>h)dataTable valuePQVSe$p7ean.e$ployeesTQ varPQite$Q borderPQ!Q
cellpaddingPQ2Q cellspacingPQ!Q
rulesPQallQ stylePQborder)solid 1p.Q<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQE$ployee %dQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.e$ployee%dTQ*<
>*h)colu$n<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQGirst :a$eQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.&irst:a$eTQ*<
>*h)colu$n<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQ4ast :a$eQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.last:a$eTQ*<
>*h)colu$n<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQE$ailQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.e$ailTQ*<
>*h)colu$n<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQ2hone :u$berQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.phone:u$berTQ*<
>*h)colu$n<
>h)colu$n<
>&)&acet na$ePQheaderQ<
>h)outputTe.t valuePQ"ire DateQ*<
>*&)&acet<
>h)outputTe.t valuePQVSite$.hireDateTQ<
>&)convertDateTi$e patternPQCC*dd*yyyy "")$$)ssQ *<
>*h)outputTe.t<
>*h)colu$n<
>*h)dataTable<
>*h)panelIroup<
>*h)&or$<
>*h)body<
>*ht$l<
>*&)view<
6H. You are done with the basic application. #ight clic+ dept.-s& and choose #un to run the
application. :ow( %DE will start integrated 041 instance 8i& not running9 and deploy the
application and will run the page.
6J. You can enhance this application to add $ore &eatures such as C#5D 8Create #ead
5pdate Delete9 operations on depart$ents and e$ployees and $any $ore.