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.
- Right-click the Source Packages node and select New > Other to open the New File wizard.
- Select Hibernate from the Categories list and HibernateUtil.java from the File Types list. Click Next.
- Type HibernateUtil for the class name and dvdrental for the package. Click Finish.
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.
- Right-click the Source Packages node in the Projects window and choose New > Other to open the New File wizard.
- Select Hibernate Reverse Engineering Wizard in the Hibernate category. Click Next.
- Specify hibernate.reveng as the File Name and src\java for the Folder (both are default). Click Next.
- Select hibernate.cfg.xml from the Configuration File drop down list, if not selected.
- Select the following tables from Available Tables and click Add to add the tables to Selected Tables.
- actor
- category
- film
- film_actor
- film_category
- language
------------------------------------------------------------------
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.
- Right-click the Source Packages node in the Projects window and choose New > Other to open the New File wizard.
- Select Hibernate Mapping Files and POJOs from a Database in the Hibernate category. Click Next.
- Ensure that the hibernate.cfg.xml and hibernate.reveng.xml files are selected in the drop down lists.
- Select JDK 5 Language Features under the General Settings options.
- Ensure that the Domain Code and Hibernate XML Mappings options are selected.
- Select dvdrental for the Package name. Click Finish.
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.
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.
No comments:
Post a Comment