We can use hibernate in the web application. By using hibernate in the web application we use the NetBeans IDE to create and deploy a web application that displays data from a database. Hibernate uses web applications as the persistence layer and also used for retrieving and storing Plain Old Java Objects (POJOs) to a relational database. Hibernate framework provides tools for object relational mapping (ORM).

Creating database

First we are discuss how to create database for the web application process. Here we are using MySQL database called sakila. It’s a free database. By using Plugins manager we are installing Sakila.  After installing plugin sakila 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.
  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.
  3. Right-click the MySQL Server node and choose Create Database.
  4. Select the Sakila database from the New Database Name drop down list in the Create MySQL Database dialog box. Click OK.
MYSQL Database

MYSQL Database

When you click OK a Sakila node appears under the MySQL Server node.

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

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.

Creating Hibernate Web application

We can create web applications and add the hibernate libraries to the project. For creating the web application project we select hibernate in the frameworks panel of the New Project wizard and specify the database. The following steps are used for creating the web application project.

READ  Configuring Hibernate

1) Choose File > New Project ( Ctrl-Shift-N; ⌘-Shift-N on Mac) from the main menu. Select Web Application from the Java Web category and click Next.

2) Type DVDStore for the project name and set the project location.

3) Deselect the Use Dedicated Folder option, if selected. Click Next.

For this tutorial there is little reason to copy project libraries to a dedicated folder because you will not need to share libraries with other users.

4) Set the server to the GlassFish Server 3 and set the Java EE Version to Java EE 6 Web. Click Next.

5) Select the JavaServer Faces checkbox and use the default JSF 2.0 libraries.

6) Select the Hibernate 3.2.5 checkbox.

7) Select the sakila database from the Database Connection drop down list. Click Finish.

If the sakila database is not available in the framework panel, we should create the database connection.

New Web Application

New Web Application

If you click Finish, the IDE creates the web application project and opens the hibernate.cfg.xml file and index.xhtml in the editor.

If you extend the Libraries node in the Projects window, It handle the IDE added the Hibernate libraries to the project.

Hibernate Libraries

Hibernate Libraries

When you create a new project that uses the hibernate framework the IDE automatically creates the hibernate.cfg.xml configuaration file at the root of the context classpath of the application (in the Files window, src/java). The file is located in the <default package> under the Source Packages node in the Projects window. This configuration file consists of  database connection, resource mappings, and other connection properties. You can edit the file using the multi-view editor, or edit the XML directly in the XML editor.  To edit the default properties specified in hibernate.cfg.xml to enable debug logging for SQL statements and to enable Hibernate’s session context management we are using the following step.

READ  Hibernate Component Mapping

1.         Open hibernate.cfg.xml in the Design tab. You can open the file by expanding the <default package> node under Source Packages in the Projects window and double-clicking hibernate.cfg.xml.

2.         In the multi-view XML editor, expand the Configuration Properties node under Optional Properties.

3.         Click Add to open the Add Hibernate Property dialog box.

4.         In the dialog box, select the hibernate.show_sql property and set the value to true. This enables the debug logging of the SQL statements.

5.         Expand the Miscellaneous Properties node and click Add.

6.         In the dialog box, select the properties hibernate.current_session_context_class and set the value to thread to enable Hibernate’s automatic session context management.

7.         Click Add again under the Miscellaneous Properties node and select hibernate.query.factory_class in the Property Name dropdown list.

8.         Select org.hibernate.hql.classic.ClassicQueryTranslatorFactory as the Property Value. Click OK.

You should type org.hibernate.hql.classic.ClassicQueryTranslatorFactory as the Property Value. In NetBeans IDE 6.9, the Property Value in the dropdown list for the factory class is incorrect.

If you click the XML tab in the editor you can see the file in XML view. Your file should look like the following (the three new properties are bold):

&lt;session-factory name="session1"&gt;
&lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
&lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/sakila&lt;/property&gt;
&lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
&lt;<strong>property</strong> name="hibernate.show_sql"&gt;true&lt;/<strong>property</strong>&gt;
&lt;<strong>property</strong> name="hibernate.current_session_context_class"&gt;thread&lt;/<strong>property</strong>&gt;
&lt;<strong>property</strong> name="hibernate.query.factory_class"&gt;org.hibernate.hql.classic.ClassicQueryTranslatorFactory&lt;/<strong>property</strong>&gt;

9.         Save your changes to the file.

If  you do not need to edit the file again you must close the file.