Using Hibernate in a Web Application 1

(With NetBeans & MySQL)

 

 

 

 

The original tutorial can be found at: Using Hibernate in a Web Application. This is a refined version with details steps.

 

In this tutorial, you use the NetBeans IDE to create and deploy a web application that displays data from a database. The web application uses the Hibernate framework as the persistence layer for retrieving and storing plain old Java objects (POJOs) to a relational database.

Hibernate is framework that provides tools for object relational mapping (ORM). The tutorial demonstrates how to add support for the Hibernate framework to the IDE and create the necessary Hibernate files. After creating the Java objects and configuring the application to use Hibernate, you create a JSF managed bean and JSF 2.0 pages to display the data. Before starting this tutorial you may want to familiarize yourself with the following documents.

 

  1. Hibernate documentation at hibernate.org (http://hibernate.org/).

  2. Introduction to Developing Web Applications.

  3. Introduction to JavaServer Faces 2.0.

 

Note. This tutorial uses NetBeans IDE 6.8. If you are using NetBeans IDE 6.7, please see Using Hibernate in a Web Application in NetBeans IDE 6.7. If you are using NetBeans IDE 6.5, please see Using Hibernate in a Web Application in NetBeans IDE 6.5.

 

Contents

 

  1. Creating the Database

  2. Creating the Web Application Project

  3. Modifying the Hibernate Configuration File

  4. Creating the HibernateUtil.java Helper File

  5. Generating Hibernate Mapping Files and Java Classes

  6. Creating the FilmHelper.java Helper Class

  7. Creating the JSF Managed Bean

  8. Creating the Web Pages

  9. Running the Project

  10. Downloading a Complete Project

 

To follow this tutorial, you need the following software and resources.

 

Software or Resource

Version Required

NetBeans IDE

6.8, Java version

Java Development Kit (JDK)

Version 6

Sun GlassFish Enterprise Server

v3

MySQL database server

Version 5.x

Sakila Database

Plugin available from update center

 

Notes:

 

  1. GlassFish v3 requires Java Development Kit (JDK) 6.

  2. If you are using GlassFish v2 or an older version of NetBeans IDE, see Using Hibernate in a Web Application - NetBeans IDE 6.7 Tutorial.

 

 

 

 

Creating the Database

 

This tutorial uses a MySQL database called sakila, a free sample MySQL database that is available from the MySQL site. The sakila database is not included when you install the IDE so you need to first create the database to follow this tutorial.

To create the sakila database you can download and install the Sakila Sample Database plugin using the Plugins manager. After you install the plugin the sakila database is added to the list of databases in the Create MySQL database dialog box.

 

1.      Open the Plugins manager and install the Sakila Sample Database plugin.

 

Opening the NetBeans Plugins manager

 

The NetBeans plugs-in manager

 

 

 

 

The NetBeans IDE plugin installer

 

The NetBeans IDE license agreement

 

The NetBeans IDE Installer plugin download in progress

 

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

 

 

 

 

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

 

NetBeans IDE Installer - plugins installation was completed successfully

 

2.      After installing the plugin, start the MySQL database by expanding the Databases node in the Services window, right-clicking the MySQL Server node and choosing Start.

 

Starting MySQl server in NetBeans

 

3.      Right-click the MySQL Server node and choose Create Database.

 

Creating database using MySQL from NetBeans IDE

 

4.      Select the Sakila database from the New Database Name drop down list in the Create MySQL Database dialog box. Click OK.

 

Using the sakila database as database template

 

When you click OK a Sakila node appears under the MySQL Server node. If you have been asked to create tables for the already available Sakila sample database, then click Yes.

 

Sakila database is visible in the NetBeans

 

5.      Right-click the Sakila node and choose Connect.

 

Connecting to sakila database from NetBeans IDE

 

When you click Connect a database connection node for the Sakila database (jdbc:mysql://localhost:3306/sakila [username on Default]) is listed under the Databases node. When a connection is open you can view the data in the database by expanding the connection node.

 

Providing the sakila database username and password credential

 

The bold database name means it is connected as shown in the following screenshot for sakila database.

 

The sakila database has been connected

 

 

 

 

 


 Back to Main | Java, ORM & Hibernate 2