ÿþ<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=unicode" /> <meta http-equiv="Content-Language" content="en-us" /> <style> <!-- p.MsoNormal, li.MsoNormal {margin-top:0mm; margin-right:0mm; margin-bottom:10.0pt; margin-left:0mm; line-height:115%; font-size:12.0pt; font-family:"Times New Roman","serif";} h1 {margin-right:0mm; margin-left:0mm; font-size:24.0pt; font-family:"Times New Roman","serif"; font-weight:bold} a:link {color:blue; text-decoration:underline;} a:visited {color:purple; text-decoration:underline;} p {margin-right:0mm; margin-left:0mm; font-size:12.0pt; font-family:"Times New Roman","serif";} ol {margin-bottom:0mm;} --> </style> <title>Creating the FilmHelper.java Helper Class</title> <meta name="keywords" content="Java, opensource, framework, examples, programs, programming, samples, database, MySQL, online" /> <meta name="description" content="Steps on how to create the FilmHelper.java Helper Class for Hibernate web applications with screenshots" /> </head> <body lang="EN-US" link="#0000FF" vlink="#800080" topmargin="20" leftmargin="20" rightmargin="20" bottommargin="20"> <div class="WordSection1"> <h1 align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><b> <font size="6" face="Arial"><span style="font-weight:bold">Using Hibernate in a Web Application 4</span></font></b></h1> <h1 align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><b> <font size="5" face="Arial"> <span style="font-weight:bold; font-style:italic">(With NetBeans &amp; MySQL)</span></font></b></h1> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0" align="center"><script src="http://tag.contextweb.com/TagPublish/getjs.aspx?action=VIEWAD&cwrun=200&cwadformat=728X90&cwpid=527221&cwwidth=728&cwheight=90&cwpnet=1&cwtagid=85440"></script></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><b><font size="3" face="Arial"> <span style="font-size:12.0pt;font-weight:bold">Creating the </span></font> <font face="Courier New"> <span style="font-family:&quot;Arial&quot;;font-weight:bold">FilmHelper.java</span></font></b><font face="Arial"><b><span style="font-weight:bold"> Helper Class</span></b></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">You will now create a helper class in the </span></font> <font face="Courier New"><span style="font-family:&quot;Arial&quot;">dvdrental</span></font><font face="Arial"> package that will be used to perform Hibernate queries on the database. You will use the <b>Hibernate Query Language</b> (HQL) editor to construct and test the queries for retrieving data. After you test the queries you will create methods in the helper class that construct and run the queries. You will then invoke the methods in the helper class from a JSF managed bean.</font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><b><font size="3" face="Arial"> <span style="font-size:12.0pt;font-weight:bold">Creating the Class</span></font></b></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">In this section you use the New File wizard to create the helper class </span> </font><font face="Courier New"><span style="font-family:&quot;Arial&quot;"> FilmHelper.java</span></font><font face="Arial"> in the </font> <font face="Courier New"><span style="font-family:&quot;Arial&quot;">dvdrental</span></font><font face="Arial"> package. You will create a Hibernate session by calling </font> <font face="Courier New"><span style="font-family:&quot;Arial&quot;">getSessionFactory</span></font><font face="Arial"> in </font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">HibernateUtil.java</span></font><font face="Arial"> and create some helper methods to create queries to retrieve data from the database. You will invoke the helper methods from the JSP pages.</font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="1" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Right-click the </span></font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">dvdrental</span></font><font face="Arial"> source package node and select New &gt; Java Class to open the New File wizard.</font></p> </li> </ol> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="587" height="347" src="hibernateormnetbean_files/javahibernateormdbaseweb035.png" alt="Adding new Java class into the Java web application" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="2" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Type <b> <span style="font-weight:bold">FilmHelper</span></b> for the class name. </span></font></p> </li> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Check that the Package is dvdrental. Click Finish.</span></font></p> </li> </ol> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="554" height="346" src="hibernateormnetbean_files/javahibernateormdbaseweb036.png" alt="Setting new Java class name and location" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="4" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Adding the following code (in bold) to create a Hibernate session.</span></font></p> </li> </ol> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;">public class FilmHelper {</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;">&nbsp;&nbsp;&nbsp; <b><span style="font-weight:bold">Session session = null;</span></b></span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"><b> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:bold">&nbsp;</span></font></b></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"><b> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:bold">&nbsp;&nbsp;&nbsp; public FilmHelper() {</span></font></b></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"><b> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:bold">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.session = HibernateUtil.getSessionFactory().getCurrentSession();</span></font></b></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"><b> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:bold">&nbsp;&nbsp;&nbsp; }</span></font></b></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:54.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;">}</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="5" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Fix your imports (</span></font><font face="Courier New"><span style="font-family:&quot;Arial&quot;; font-weight:700">org.hibernate.Session</span></font><font face="Arial">) and save your changes.</font></p> </li> </ol> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="488" height="257" src="hibernateormnetbean_files/javahibernateormdbaseweb037.png" alt="Fixing the import in the Java web source code" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="353" height="200" src="hibernateormnetbean_files/javahibernateormdbaseweb038.png" alt="Fixing all the unresolved imports" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">You will now modify </span></font><font face="Courier New"> <span style="font-family: &quot;Arial&quot;">FilmHelper.java</span></font><font face="Arial"> to add methods that query the DB.</font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><b><font size="3" face="Arial"> <span style="font-size:12.0pt;font-weight:bold">Enumerating Film Titles and Retrieving Actors Using an HQL Query</span></font></b></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">In this exercise you will create a Hibernate Query Language (HQL) query that queries the database to retrieve a list of film titles from the Film table. You will then add a method that queries both the Actor and Film_actor tables to fetch the actors involved in a particular film.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">The Film table has 1000 records so the method to retrieve the list of films should be able to retrieve records based on the </span></font> <font face="Courier New"> <span style="font-family:&quot;Arial&quot;; font-weight:700">filmId</span></font><font face="Arial"> primary key. You will use the HQL editor to construct and test the HQL query. After you have created the correct query you will add a method to the class that can generate the proper query.</font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="1" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Right-click </span></font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">hibernate.cfg.xml</span></font><font face="Arial"> in the Projects window and choose Run HQL Query to open the HQL query editor.</font></p> </li> </ol> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="319" height="266" src="hibernateormnetbean_files/javahibernateormdbaseweb039.png" alt="Running the HQL Query in NetBeans IDE" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="2" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Select <b>hibernate.cfg</b> from the drop down list in the toolbar.</span></font></p> </li> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Test the connection by typing the following in the editor and clicking the Run HQL Query button ( <img border="0" width="16" height="16" id="Picture 81" src="hibernateormnetbean_files/javahibernateormdbaseweb040.png" alt="Run HQL Query button" /> ) in the toolbar.</span></font></p> </li> </ol> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:36.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;">from Film</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:36.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Arial"><span style="font-size:12.0pt">When you click Run HQL Query you can see the results of the query in the bottom window of the HQL query editor.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="696" height="418" src="hibernateormnetbean_files/javahibernateormdbaseweb041.png" alt="The HQL Query result set" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:36.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Arial"><span style="font-size:12.0pt">If you click the SQL button you can see the equivalent SQL query.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:36.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700"> select film0_.film_id as col_0_0_ from sakila.film film0_</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0" align="center"><script type="text/javascript"><!-- google_ad_client = "pub-8089415323104206"; /* 300x250, created 5/16/08javaos */ google_ad_slot = "8146009330"; google_ad_width = 300; google_ad_height = 250; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0" align="center"><font face="Arial"> --------------------------------------------------------------</font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0">&nbsp;</p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="435" height="239" src="hibernateormnetbean_files/javahibernateormdbaseweb042.png" alt="The HQL statement" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="4" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Type the following query to retrieve the records in the Film table where the film id is between 100 and 200.</span></font></p> </li> </ol> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:36.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;">from Film as film where <b>film.filmId</b> between 100 and 200</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="717" height="420" src="hibernateormnetbean_files/javahibernateormdbaseweb043.png" alt="Doing another HQL Query" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:36.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Arial"><span style="font-size:12.0pt">The result window displays a list of records. Now that you have tested that the query returns the desired results, you can use the query in the helper class.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="5" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Add the following method </span></font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">getFilmTitles</span></font><font face="Arial"> to </font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">FilmHelper.java</span></font><font face="Arial"> to retrieve the films where the film id is between a certain range specified by the variables </font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">startID</span></font><font face="Arial"> and </font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">endID</span></font><font face="Arial">.</font></p> </li> </ol> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700"> public List getFilmTitles(int startID, int endID) {</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; List&lt;Film&gt; filmList = null;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; try {</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; org.hibernate.Transaction tx = session.beginTransaction();</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Query q = session.createQuery (&quot;from Film as film where film.filmId between &#39;&quot;+startID+&quot;&#39; and &#39;&quot;+endID+&quot;&#39;&quot;);</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filmList = (List&lt;Film&gt;) q.list();</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; } catch (Exception e) {</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; }</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; return filmList;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:54.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;">}</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="620" height="276" src="hibernateormnetbean_files/javahibernateormdbaseweb044.png" alt="Adding new method to the class" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="6" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Fix your imports and save your changes.&nbsp; When you fix your imports you want to choose </span></font><font face="Courier New"> <span style="font-family: &quot;Arial&quot;">java.util.List</span></font><font face="Arial"> and </font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">org.hibernate.Query</span></font><font face="Arial">.</font></p> </li> </ol> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="422" height="186" src="hibernateormnetbean_files/javahibernateormdbaseweb045.png" alt="Fixing import" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="507" height="228" src="hibernateormnetbean_files/javahibernateormdbaseweb046.png" alt="Fixing all the unresolved import errors in the source file" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <ol style="margin-top:0mm" start="7" type="1"> <li class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt;line-height: normal"> <p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <font size="3" face="Arial"><span style="font-size:12.0pt">Add the following method </span></font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">getActorsByID</span></font><font face="Arial"> that retrieves the actors in a particular film. The method constructs the query using </font><font face="Courier New"> <span style="font-family:&quot;Arial&quot;">filmId</span></font><font face="Arial"> as the input variable.</font></p> </li> </ol> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700"> public List getActorsByID(int filmId){</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; List&lt;Actor&gt; actorList = null;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; try {</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; org.hibernate.Transaction tx = session.beginTransaction();</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Query q = session.createQuery (&quot;from Actor as actor where actor.actorId in (select filmActor.actor.actorId from FilmActor as filmActor where filmActor.film.filmId=&#39;&quot; + filmId + &quot;&#39;)&quot;);</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; actorList = (List&lt;Actor&gt;) q.list();</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; } catch (Exception e) {</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; }</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:18.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700">&nbsp;&nbsp;&nbsp; return actorList;</span></font></p> <p class="MsoNormal" style="margin-top:0;margin-right:0mm;margin-bottom:0mm; margin-left:54.0pt;margin-bottom:0;line-height:100%"> <font size="3" face="Courier New"> <span style="font-size:12.0pt;font-family:&quot;Arial&quot;; font-weight:700"> }</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center;line-height:100%; margin-top:0"><font size="3" face="Arial"> <span style="font-size:12.0pt"> <img border="0" width="482" height="305" src="hibernateormnetbean_files/javahibernateormdbaseweb047.png" alt="Adding method getActorsByID that retrieves the actors in a particular film" /></span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0;line-height: 100%; margin-top:0"><font size="3" face="Arial"><span style="font-size:12.0pt">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0; line-height:100%; margin-top:0"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0; line-height:100%; margin-top:0"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%">&nbsp;</span></font></p> <hr color="#FF0000" /> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0; line-height:100%; margin-top:0" align="center"> <font face="Arial" size="5"> <a title="Creating the HibernateUtil.java Helper File" target="_top" href="http://www.visualcplusdotnet.com/javaopensource/hibernateormnetbean2.html"> Java, ORM &amp; Hibernate 3</a> | <a title="The Java and open source applications" target="_top" href="http://www.javaguicodexample.com/"> Back to Main</a> | <a title="Adding Additional Helper Methods" target="_top" href="http://www.visualcplusdotnet.com/javaopensource/hibernateormnetbean4.html"> Java, ORM &amp; Hibernate 5</a></font></p> <p class="MsoNormal" style="line-height: 100%; margin-top: 0; margin-bottom: 0">&nbsp;</p> <p class="MsoNormal" style="line-height: 100%; margin-top: 0; margin-bottom: 0" align="center"><script type="text/javascript"><!-- google_ad_client = "pub-8089415323104206"; /* 728x90, created 5/16/08 javaos */ google_ad_slot = "1917524460"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script></p> </div> </body> </html>