Using Hibernate in a Web Application 4

(With NetBeans & MySQL)

 

 

 

 

Creating the FilmHelper.java Helper Class

 

You will now create a helper class in the dvdrental package that will be used to perform Hibernate queries on the database. You will use the Hibernate Query Language (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.

 

Creating the Class

 

In this section you use the New File wizard to create the helper class FilmHelper.java in the dvdrental package. You will create a Hibernate session by calling getSessionFactory in HibernateUtil.java and create some helper methods to create queries to retrieve data from the database. You will invoke the helper methods from the JSP pages.

 

  1. Right-click the dvdrental source package node and select New > Java Class to open the New File wizard.

 

Adding new Java class into the Java web application

 

  1. Type FilmHelper for the class name.

  2. Check that the Package is dvdrental. Click Finish.

 

Setting new Java class name and location

 

  1. Adding the following code (in bold) to create a Hibernate session.

 

public class FilmHelper {

 

    Session session = null;

 

    public FilmHelper() {

        this.session = HibernateUtil.getSessionFactory().getCurrentSession();

    }

 

}

 

  1. Fix your imports (org.hibernate.Session) and save your changes.

 

Fixing the import in the Java web source code

 

Fixing all the unresolved imports

 

You will now modify FilmHelper.java to add methods that query the DB.

 

Enumerating Film Titles and Retrieving Actors Using an HQL Query

 

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.

The Film table has 1000 records so the method to retrieve the list of films should be able to retrieve records based on the filmId 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.

 

  1. Right-click hibernate.cfg.xml in the Projects window and choose Run HQL Query to open the HQL query editor.

 

Running the HQL Query in NetBeans IDE

 

  1. Select hibernate.cfg from the drop down list in the toolbar.

  2. Test the connection by typing the following in the editor and clicking the Run HQL Query button ( Run HQL Query button ) in the toolbar.

 

from Film

 

When you click Run HQL Query you can see the results of the query in the bottom window of the HQL query editor.

 

The HQL Query result set

 

If you click the SQL button you can see the equivalent SQL query.

 

select film0_.film_id as col_0_0_ from sakila.film film0_

 

 

 

 

 

--------------------------------------------------------------

 

The HQL statement

 

  1. Type the following query to retrieve the records in the Film table where the film id is between 100 and 200.

 

from Film as film where film.filmId between 100 and 200

 

 

Doing another HQL Query

 

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.

 

  1. Add the following method getFilmTitles to FilmHelper.java to retrieve the films where the film id is between a certain range specified by the variables startID and endID.

 

public List getFilmTitles(int startID, int endID) {

    List<Film> filmList = null;

    try {

        org.hibernate.Transaction tx = session.beginTransaction();

        Query q = session.createQuery ("from Film as film where film.filmId between '"+startID+"' and '"+endID+"'");

        filmList = (List<Film>) q.list();

    } catch (Exception e) {

        e.printStackTrace();

    }

    return filmList;

}

 

Adding new method to the class

 

  1. Fix your imports and save your changes.  When you fix your imports you want to choose java.util.List and org.hibernate.Query.

 

Fixing import

 

Fixing all the unresolved import errors in the source file

 

  1. Add the following method getActorsByID that retrieves the actors in a particular film. The method constructs the query using filmId as the input variable.

 

public List getActorsByID(int filmId){

    List<Actor> actorList = null;

    try {

        org.hibernate.Transaction tx = session.beginTransaction();

        Query q = session.createQuery ("from Actor as actor where actor.actorId in (select filmActor.actor.actorId from FilmActor as filmActor where filmActor.film.filmId='" + filmId + "')");

        actorList = (List<Actor>) q.list();

 

    } catch (Exception e) {

        e.printStackTrace();

    }

 

    return actorList;

}

 

Adding method getActorsByID that retrieves the actors in a particular film

 

 

 


Java, ORM & Hibernate 3 | Back to Main | Java, ORM & Hibernate 5