Using Hibernate in a Web Application 3

(With NetBeans & MySQL)

 

 

 

 

 

Creating the HibernateUtil.java Helper File

 

To use Hibernate you need to create a helper class that handles startup and that accesses Hibernate's SessionFactory to obtain a Session object. The class calls configure() and loads the hibernate.cfg.xml configuration file and then builds the SessionFactory to obtain the Session object. In this section you use the New File wizard to create the helper class HibernateUtil.java.

 

  1. Right-click the Source Packages node and select New > Other to open the New File wizard.

 

Adding Java package into Hibernate web application project

 

  1. Select Hibernate from the Categories list and HibernateUtil.java from the File Types list. Click Next.

 

Adding the hibernateUtil.java file into the Hibernate Java web application project

 

  1. Type HibernateUtil for the class name and dvdrental for the package. Click Finish.

 

Do some settings on the HibernateUtil.java

 

When you click Finish, HibernateUtil.java opens in the editor. You can close the file because you do not need to edit the file.

 

Generating Hibernate Mapping Files and Java Classes

 

In this tutorial you use a POJO (plain old Java object) to represent the data in each of the tables in the database that you will use. The Java class specifies the fields for the columns in the tables and uses simple setters and getters to retrieve and write the data. To map the POJOs to the tables you can use a Hibernate mapping file or use annotations in the class.

You can use the Hibernate Mapping Files and POJOs from a Database wizard to create multiple POJOs and mapping files based on database tables. When you use the wizard you select all the tables for which you want POJOs and mapping files and the IDE then generates the files for you based on the database tables and adds the mapping entries to hibernate.cfg.xml. When you use the wizard you can choose the files that you want the IDE to generate (only the POJOs, for example) and select code generation options (generate code that uses EJB 3 annotations, for example).

Note. The IDE also has wizards to help you create individual POJOs and mapping files from scratch.

 

Creating the Hibernate Reverse Engineering File

 

If you want to use the Hibernate Mapping Files and POJOs from a Database wizard, you first need to create a hibernate.reveng.xml reverse engineering file. The Hibernate Mapping Files and POJOs from a Database wizard requires hibernate.reveng.xml and hibernate.cfg.xml.

The reverse engineering file enables you to have greater control over how database mapping strategy. The Hibernate Reverse Engineering Wizard creates a reverse engineering file with a default configuration that you can edit in the XML editor.

To create the Hibernate reverse engineering file, perform the following steps.

 

  1. Right-click the Source Packages node in the Projects window and choose New > Other to open the New File wizard.

 

  1. Select Hibernate Reverse Engineering Wizard in the Hibernate category. Click Next.

 

Adding another file - Hibernate Reverse Engineering Wizard

 

  1. Specify hibernate.reveng as the File Name and src\java for the Folder (both are default). Click Next.

 

Setting the name and location for the Hibernate Reverse Engineering Wizard

 

  1. Select hibernate.cfg.xml from the Configuration File drop down list, if not selected.

  2. Select the following tables from Available Tables and click Add to add the tables to Selected Tables.

  1. actor

  2. category

  3. film

  4. film_actor

  5. film_category

  6. language

 

 

 

 

 

 

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

 

Selecting the database tables into the Hibernate Reverse Engineering Wizard

 

Click Finish.

 

The wizard generates a hibernate.reveng.xml reverse engineering file and opens the file in the editor. You can close the reverse engineering file because you will not need to edit the file.

 

Creating the Hibernate Mapping Files and POJOs

 

You can use the Hibernate Mapping Files and POJOs from a Database wizard to generate files for you. The wizard can generate a POJO and a corresponding mapping file for each table that you select in the wizard. The mapping files are XML files that contain data about how the columns in the tables are mapped to the fields in the POJOs. You need to have the hibernate.reveng.xml and hibernate.cfg.xml files to use the wizard.

To create the POJOS and mapping files using a wizard, perform the following steps.

 

  1. Right-click the Source Packages node in the Projects window and choose New > Other to open the New File wizard.

  2. Select Hibernate Mapping Files and POJOs from a Database in the Hibernate category. Click Next.

 

Adding more Hibernate files - Hibernate Mapping Files and POJOs from Database

 

  1. Ensure that the hibernate.cfg.xml and hibernate.reveng.xml files are selected in the drop down lists.

  2. Select JDK 5 Language Features under the General Settings options.

  3. Ensure that the Domain Code and Hibernate XML Mappings options are selected.

  4. Select dvdrental for the Package name. Click Finish.


Doing some settings on the Hibernate Mapping Files and POJOs from Database

 

When you click Finish the IDE generates POJOs and Hibernate mapping files with the fields mapped to the columns specified in hibernate.reveng.xml. The IDE also adds mapping entries to hibernate.cfg.xml. You can expand the dvdrental package to see the files generated by the wizard.

 

The dvdrental package with the files generated by the wizard

 

You can use the Hibernate Mapping wizard if you want to create a Hibernate mapping file that maps a specific table to a specific class.

 

 

 


Java, ORM & Hibernate 2 | Back to Main | Java, ORM & Hibernate 4