Date post: | 02-Jun-2018 |
Category: |
Documents |
Upload: | pavan-kumar |
View: | 219 times |
Download: | 0 times |
of 24
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
1/24
Using Hibernate in a Web Application
In this tutorial, you use the NetBeans IDE to create and deploy a webapplication that displays data from a database !he web application
uses the Hibernate framewor" as the persistence layer for retrie#ingand storing plain old $a#a ob%ects &'($(s) to a relational databaseHibernate is framewor" that pro#ides tools for ob%ect relationalmapping &(*+) !he tutorial demonstrates how to add support for the
Hibernate framewor" to the IDE and create the necessary Hibernatefiles After creating the $a#a ob%ects and configuring the application touse Hibernate, you add isual Web $-. components to a web page todisplay the data
Before starting this tutorial you may want to familiari/e yourself with
using HibernateHibernate documentation at hibernateorg
Note.!his tutorial uses NetBeans IDE 01 If you are using NetBeans
IDE 02, please see Using Hibernate in a Web Application in NetBeansIDE 02
Contents
3reating the Database
3reating the Web Application 'ro%ect
+odifying the Hibernate 3onfiguration .ile
3reating the HibernateUtil.java Hibernate Helper .ile
4enerating Hibernate +apping .iles and $a#a 3lasses
3reating the FilmHelper.java Helper 3lass3reating the 3lass
Enumerating .ilm !itles and *etrie#ing Actors Using an H56
5uery
Adding Additional Helper +ethods
3reating the Web 'ages
+odifying index.jsp
3reating browse.jsp
*unning the 'ro%ect
http://www.hibernate.org/http://netbeans.org/kb/65/web/hibernate-webapp.htmlhttp://netbeans.org/kb/65/web/hibernate-webapp.htmlhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#01http://netbeans.org/kb/docs/web/hibernate-webapp.html#02http://netbeans.org/kb/docs/web/hibernate-webapp.html#03http://netbeans.org/kb/docs/web/hibernate-webapp.html#04http://netbeans.org/kb/docs/web/hibernate-webapp.html#04http://netbeans.org/kb/docs/web/hibernate-webapp.html#04http://netbeans.org/kb/docs/web/hibernate-webapp.html#04http://netbeans.org/kb/docs/web/hibernate-webapp.html#05http://netbeans.org/kb/docs/web/hibernate-webapp.html#06http://netbeans.org/kb/docs/web/hibernate-webapp.html#06http://netbeans.org/kb/docs/web/hibernate-webapp.html#06http://netbeans.org/kb/docs/web/hibernate-webapp.html#06http://netbeans.org/kb/docs/web/hibernate-webapp.html#06ahttp://netbeans.org/kb/docs/web/hibernate-webapp.html#06bhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#06bhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#06dhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#07http://netbeans.org/kb/docs/web/hibernate-webapp.html#07ahttp://netbeans.org/kb/docs/web/hibernate-webapp.html#07ahttp://netbeans.org/kb/docs/web/hibernate-webapp.html#07bhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#07bhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#08http://netbeans.org/kb/65/web/hibernate-webapp.htmlhttp://netbeans.org/kb/65/web/hibernate-webapp.htmlhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#01http://netbeans.org/kb/docs/web/hibernate-webapp.html#02http://netbeans.org/kb/docs/web/hibernate-webapp.html#03http://netbeans.org/kb/docs/web/hibernate-webapp.html#04http://netbeans.org/kb/docs/web/hibernate-webapp.html#05http://netbeans.org/kb/docs/web/hibernate-webapp.html#06http://netbeans.org/kb/docs/web/hibernate-webapp.html#06ahttp://netbeans.org/kb/docs/web/hibernate-webapp.html#06bhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#06bhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#06dhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#07http://netbeans.org/kb/docs/web/hibernate-webapp.html#07ahttp://netbeans.org/kb/docs/web/hibernate-webapp.html#07bhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#08http://www.hibernate.org/8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
2/24
Downloading the -olution 'ro%ect
To follow this tutorial, you need the following software and
resources.
Software or Resource Version Required
NetBeans IDE 01, $a#a #ersion
$a#a De#elopment 7it &$D7) ersion 0 or #ersion 2
Apache !omcat 089:
+y-56 database ser#er ersion 2;
-a"ila Database 'lugin a#ailable from update center
Note.!he Apache !omcat webser#er is included with the bundleddownload of the IDE Alternati#ely, you can use the bundled 4lass.ishApplication -er#er
clic"ing the+y-56 -er#er node and choosing -tart
? *ight>clic" the +y-56 -er#er node and choose 3reate Database
http://netbeans.org/kb/docs/web/hibernate-webapp.html#08ahttp://www.netbeans.org/downloads/index.htmlhttp://java.sun.com/javase/downloads/index.jsphttp://www.mysql.com/http://kenai.com/projects/netbeans-samples/downloads/download/Samples/Web/DVDStore.ziphttp://netbeans.org/kb/docs/ide/mysql.htmlhttp://netbeans.org/kb/docs/web/hibernate-webapp.html#08ahttp://www.netbeans.org/downloads/index.htmlhttp://java.sun.com/javase/downloads/index.jsphttp://www.mysql.com/http://kenai.com/projects/netbeans-samples/downloads/download/Samples/Web/DVDStore.ziphttp://netbeans.org/kb/docs/ide/mysql.html8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
3/24
@ -elect the -a"ila database from the New Database Name drop
down list in the 3reate +y-56 Database dialog bo; 3lic" (7
When you clic" (7 a -a"ila node appears under the +y-56 -er#ernode
2 *ight>clic" the -a"ila node and choose 3onnect
When you clic" 3onnect a database connection node for the -a"iladatabase &jdbc:mysql://localhost:330/sakila !usernameon
"e#a$lt%) is listed under the Databases node When a connection is
open you can #iew the data in the database by e;panding theconnection node
3reating the Web Application 'ro%ect
In this e;ercise you will create a web application pro%ect and add the
Hibernate libraries to the pro%ect When you create the pro%ect, you willselect Hibernate in the .ramewor"s panel of the New 'ro%ect wi/ard
and specify the database9 3hoose .ile New 'ro%ect &3trl>-hift>N) -elect Web Application
from the $a#a Web category and clic" Ne;t
= !ype DVDStorefor the pro%ect name and set the pro%ect location
? Deselect the Use Dedicated .older option, if selected.or this tutorial there is little reason to copy pro%ect libraries to a
dedicated folder because you will not need to share libraries withother users
3lic" Ne;t
@ -et the ser#er to Apache !omcat 089: and set the $a#a EEersion to $a#a EE 2 3lic" Ne;t
2 -elect the Hibernate ?=2 chec"bo;
0 -elect the sa"ila database from the Database 3onnection drop
down list 3lic" .inish
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
4/24
Note:If the sa"ila database is not a#ailable as an option in the
.ramewor"s panel in the wi/ard, chec" to see if the connection is listedunder the Databases node in the -er#ices window If the connection is
not there, you need to create the database connection
When you clic" .inish, the IDE creates the web application pro%ect andopens the hibernate.c#&.xmlfile and index.jspin the editor
If you e;pand the 6ibraries node in the 'ro%ects window, you can seethat the IDE added the Hibernate libraries to the pro%ect
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
5/24
+odifying the Hibernate 3onfiguration .ile
When you create a new pro%ect that uses the Hibernate framewor", theIDE automatically creates the hibernate.c#&.xmlconfiguration file at
the root of the conte;t classpath of the application &in the .iles window,WEB>IN.classes) !he file is located under the 3onfiguration .iles noden the 'ro%ects window !he configuration file contains informationabout the database connection, resource mappings, and other
connection properties #iew editor,or edit the C+6 directly in the C+6 editorIn this e;ercise you will edit the default properties specifiedn hibernate.c#&.xmlto enable debug logging for -56 statements and
to enable Hibernates session conte;t management
9 (pen hibernate.c#&.xmlin the Design tab
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
6/24
management
If you clic" the C+6 tab in the editor you can see the file in C+6#iew
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
7/24
In this section you use the New .ile wi/ard to create the helper
class HibernateUtil.java
9 *ight>clic" the -ource 'ac"ages node and select New (ther toopen the New .ile wi/ard
= -elect Hibernate from the 3ategories list and HibernateUtil%a#afrom the .ile !ypes list 3lic" Ne;t
? !ype Hibernatetilfor the class name and d!drentalfor thepac"age 3lic" .inish
When you clic" .inish, HibernateUtil.javaopens in the editor
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
8/24
and select code generation options &generate code that uses E$B ?
annotations, for e;ample)Note.!he IDE also has wi/ards to help you create indi#idual '($(s and
mapping files from scratch
3reating the Hibernate *e#erse Engineering .ile9 *ight>clic" the -ource 'ac"ages node in the 'ro%ects window and
choose New (ther to open the New .ile wi/ard
= -elect Hibernate *e#erse Engineering Wi/ard in the Hibernatecategory 3lic" Ne;t
? -pecify hibernate.reven&as the .ile Name and src/javafor the
.older 3lic" Ne;t
@ -elect hibernate.c#&.xmlfrom the 3onfiguration .ile drop downlist, if not selected
2 -elect the following tables from A#ailable !ables and clic" Add toadd the tables to -elected !ables
o actor
o category
o film
o filmFactor
o filmFcategory
o language
3lic" .inish
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
9/24
!he wi/ard generates a hibernate.reven&.xmlre#erse engineering file
and opens the file in the editor
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
10/24
0 -elect d!drentalfor the 'ac"age name 3lic" .inish
When you clic" .inish the IDE generates '($(s and Hibernate mappingfiles with the fields mapped to the columns specified
n hibernate.reven&.xml !he IDE also adds mapping entries
to hibernate.c#&.xml
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
11/24
3reating the FilmHelper.javaHelper 3lass
clic" the dvdrentalsource pac"age node and select New
$a#a 3lass to open the New .ile wi/ard
. !ype &il'Hel*erfor the class name 3lic" .inish
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
12/24
-. Adding the following code &in bold) to create a Hibernate session
. p$blic class FilmHelper 5
$.
/. Session session = null;0.
1. public FilmHelper() {
2. this.session =HibernateUtil.getSessionFactory().getCurrentSession();
+3. }
++.6
+. .i; your imports &or&.hibernate.ession) and sa#e your
changes
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
13/24
If you clic" the -56 button you can see the eGui#alent -56 Gueryselect #ilm0'.#ilm'id as col'0'0' #rom sakila.#ilm#ilm0'
@ !ype the following Guery to retrie#e the records in the .ilm table
where the film id is between 988 and =88
#rom Film as #ilm where #ilm.#ilm7d between -00 and 800!he result window displays a list of records Now that you ha#e
tested that the Guery returns the desired results, you can use theGuery in the helper class
2 Add the following method &etFilm9itlesto FilmHelper.javato
retrie#e the films where the film id is between a certain rangespecified by the #ariables start7"and end7"
0 p$blic 1ist &etFilm9itlesint start7" int end7"4 5
1 1ist(Film* #ilm1ist + n$ll;: try 5
or&.hibernate.9ransaction tx +session.be&in9ransaction4;
98 $ery q + session.create$ery ,#rom Film as#ilm where #ilm.#ilm7d between
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
14/24
9@ 6
92 ret$rn #ilm1ist;6
90 Add the following method &et?ctors@y7"that retrie#es the
actors in a particular film !he method constructs the Gueryusing #ilm7das the input #ariable
91 p$blic 1ist &et?ctors@y7"int #ilm7d45
9: 1ist(?ctor* actor1ist + n$ll;
9 try 5
=8 or&.hibernate.9ransaction tx +session.be&in9ransaction4;
=9 $ery q + session.create$ery ,#rom ?ctor as
actor where actor.actor7d in select#ilm?ctor.actor.actor7d #rom Film?ctor as #ilm?ctorwhere #ilm?ctor.#ilm.#ilm7d+
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
15/24
#ilmAat.cate&ory.cate&ory7d #rom FilmAate&ory as #ilmAatwhere #ilmAat.#ilm.#ilm7d+
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
16/24
?0 lan&$a&e + 1an&$a&e4 q.$niq$eBes$lt4;
?1 6 catch >xception e4 5
?: e.printtack9race4;
? 6
@8@9 ret$rn lan&$a&e.&etCame4;
6
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
17/24
+3. int pre$-start* =+;
++. int pre$-en#* = +,;
+.
+-. int F-/0C1/*-C1U23 = +,,,;
+.+$. boolean /0C1/*-S34/3-5460 = 7alse;
+/. boolean /0C1/*-02*-5460 = 7alse;
+0.
+1. i7 (re8uest.get5arameter("starti#") 9= null) {
+2. start* =nteger.parsent(re8uest.get5arameter("starti#"));
3. }
+. i7 (re8uest.get5arameter("en#i#") 9= null) {. en#* =
nteger.parsent(re8uest.get5arameter("en#i#"));
-. }
.$. FilmHelper helper = ne: FilmHelper();
/. ist 7ilm3itles =helper.getFilm3itles(start*en#*);
0.1. i7(start* == +){
2. /0C1/*-S34/3-5460 = true;
-3. }
-+. i7(en#* == F-/0C1/*-C1U23){
-. /0C1/*-02*-5460 = true;
--. }
-.
-$. pre$-start* = start*+,;
-/. pre$-en#* = en#*+,;-0.-1. start* = en#*+;
-2. en#* = en#*+,;
3.
+. int 7ilm3itlesSi>e = 7ilm3itles.si>e();
.-. &
(/body*
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
18/24
. Add the following &in bold) to generate the table to display
the retrie#ed items
$. int #ilm9itlesiGe + #ilm9itles.siGe4;
/.0. out.print("
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
19/24
/. out.print("
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
20/24
+. }
+$. &(/body*
+/. Add the following code &in bold) to call the methods
in FilmHelper.javato retrie#e the data for the film according tofilm ID
+0. #ilm7" +7nte&er.parse7ntreq$est.&etarameter,id,44;
+1. 6
+2. D*
3.+.
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
21/24
0. int lang* =7ilm.getanguageGyanguage#().getanguage#().intalue();
1. String language = helper.getangGy*(lang*);
2. int 7ilmength = 7ilm.getength();$3. String 7ilm/ating = 7ilm.get/ating();
$+. String 7ilmIear = 7ilm.get/eleaseIear().toString();
$.$-. int rental*uration = (int)
7ilm.get/ental*uration();
$. 7loat rental/ate =7ilm.get/ental/ate().7loatalue();
$$.$/. String specialFeatures = 7ilm.getSpecialFeatures();
$0.$1. &
(/body*
$2. Add the following code &in bold) to generate the tabledisplaying the data for the film
/3. trin& specialFeat$res + #ilm.&etpecialFeat$res4;
/+. out.print("
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
22/24
03. out.print("
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
23/24
8/10/2019 Using Hibernate in a Web Application Netbean6 5 and 6 7
24/24
= In your browser, clic" +ore or the film title to
load browse.jspto #iew the film details
After testing that the application lists the film titles and displays thefilm details, you can add more functions .or e;ample, you can
create rent.jspto enable the user to rent the title