Using Hibernate in a Java Swing Application
This tutorial needs a review. You can edit it in GitHub following these contribution guidelines. |
- Creating the Database
- Creating the Java Swing Application Project
- Adding Hibernate Support to the Project
- Generating Hibernate Mapping Files and Java Classes
- Creating the Application GUI
- Creating the Query in the HQL Query Editor
- Adding the Query to the Form
- Running the Project
- Creating POJOs and Mapping Files Individually
- See Also
In this tutorial, you use the NetBeans IDE to create and deploy a Java Swing application that displays data from a database. The application uses the Hibernate framework as the persistence layer to retrieve POJOs (plain old Java objects) from a relational database.
Hibernate is framework that provides tools for object relational mapping (ORM). The tutorial demonstrates the support for the Hibernate framework included in the IDE and how to use wizards to create the necessary Hibernate files. After creating the Java objects and configuring the application to use Hibernate, you create a GUI interface for searching and displaying the data.
The application that you build in this tutorial is a companion administration application for the DVD Store web application. This tutorial covers how to create an application that allows you to query an actor’s profile based on the match with first name or last name. If you wish you can extend the application to query film details and to add/update/delete items. This tutorial uses MySQL and the Sakila database, but you can use any supported database server with Hibernate applications. The Sakila database is a sample database that you can download from the MySQL site. Information for setting up the Sakila DB is provided in the following sections.
Before starting this tutorial you may want to familiarize yourself with the following documentation.
-
Hibernate documentation at hibernate.org.
-
Connecting to a MySQL Database tutorial.
To build this application using Maven, see Creating a Maven Swing Application Using Hibernate.
To follow this tutorial, you need the following software and resources.
Software or Resource | Version Required |
---|---|
7.2, 7.3, 7.4, 8.0, Java |
|
version 7 or 8 |
|
version 5.x |
|
Sakila Database |
plugin available from update center |
You can download a zip archive of the finished project.
Creating the Database
This tutorial uses a MySQL database called sakila
. The sample database is not included when you install the IDE so you need to first create the database to follow this tutorial.
The Sakila database is a free sample MySQL database that is available from the MySQL site. To create the sakila database you can download and install the Sakila Sample Database plugin using the Plugins manager. After you install the plugin you can create the sakila database from the Services window. The sakila database is added to the list of databases in the Create MySQL database dialog box.
For more information on configuring the IDE to work with MySQL, see the Connecting to a MySQL Database tutorial.
-
Open the Plugins manager and install the Sakila Sample Database plugin.
-
After installing the plugin, start the MySQL database server by expanding the Databases node in the Services window, right-clicking the MySQL Server node and choosing Start.
-
Right-click the MySQL Server node and choose Create Database.
-
Select the Sakila database from the New Database Name drop down list in the Create MySQL Database dialog box. Click OK.
When you click OK a Sakila node appears under the MySQL Server node.
-
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 the Java Swing Application Project
In this exercise you create a simple Java Swing application project called DVDStoreAdmin.
-
Choose File > New Project (Ctrl-Shift-N). Select Java Application from the Java category and click Next.
-
Type DVDStoreAdmin for the project name and set the project location.
-
Deselect the Use Dedicated Folder option, if selected. 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.
-
Deselect Create Main Class. Click Finish.
-
When you click Finish, the IDE creates the Java application project. The project does not have a main class. You will create a form and then set the form as the main class.
Adding Hibernate Support to the Project
To add support for Hibernate to a J2SE project you need to add the Hibernate library to the project. The Hibernate library is included with the IDE and can be added to any project by right-clicking the 'Libraries' node in the Projects window, selecting 'Add Library' and then selecting the Hibernate library in the Add Library dialog box.
The IDE includes wizards to help you create the Hibernate files you may need in your project. You can use the wizards in the IDE to create a Hibernate configuration file and a utility helper class. If you create the Hibernate configuration file using a wizard the IDE automatically adds the Hibernate libraries to the project.
Creating the Hibernate Configuration File
The Hibernate configuration file ( hibernate.cfg.xml
) contains information about the database connection, resource mappings, and other connection properties. When you create a Hibernate configuration file using a wizard you specify the database connection by choosing from a list of database connection registered with the IDE. When generating the configuration file the IDE automatically adds the connection details and dialect information based on the selected database connection. The IDE also automatically adds the Hibernate library to the project classpath. After you create the configuration file you can edit the file using the multi-view editor, or edit the XML directly in the XML editor.
-
Right-click the Source Packages node in the Projects window and choose New > Other to open the New File wizard.
-
Select Hibernate Configuration Wizard from the Hibernate category. Click Next.
-
Keep the default settings in the Name and Location pane (you want to create the file in the
src
directory). Click Next. -
Select the sakila connection in the Database Connection drop down list. Click Finish.
When you click Finish the IDE opens hibernate.cfg.xml
in the source editor. The IDE creates the configuration file at the root of the context classpath of the application (in the Files window, WEB-INF/classes). In the Projects window the file is located in the <default package>
source package. The configuration file contains information about a single database. If you plan to connect to multiple databases, you can create multiple configuration files in the project, one for each database servers, but by default the helper utility class will use the hibernate.cfg.xml
file located in the root location.
If you expand the Libraries node in the Projects window you can see that the IDE added the required Hibernate JAR files and the MySQL connector JAR.
Note. NetBeans IDE 8.0 bundles the Hibernate 4 libraries. Older versions of the IDE bundled Hibernate 3.
Modifying the Hibernate Configuration File
In this exercise you will edit the default properties specified in hibernate.cfg.xml
to enable debug logging for SQL statements.
-
Open
hibernate.cfg.xml
in the Design tab. You can open the file by expanding the Configuration Files node in the Projects window and double-clickinghibernate.cfg.xml
. -
Expand the Configuration Properties node under Optional Properties.
-
Click Add to open the Add Hibernate Property dialog box.
-
In the dialog box, select the
hibernate.show_sql
property and set the value totrue
. Click OK. This enables the debug logging of the SQL statements.
-
Click Add under the Miscellaneous Properties node and select
hibernate.query.factory_class
in the Property Name dropdown list. -
Type org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory as the Property Value.
This is the translator factory class that is used in Hibernate 4 that is bundled with the IDE.
Click OK.
If you are using NetBeans IDE 7.4 or earlier you should select org.hibernate.hql.classic.ClassicQueryTranslatorFactory as the Property Value in the dialog box. NetBeans IDE 7.4 and earlier bundled Hibernate 3.
If you click the XML tab in the editor you can see the file in XML view. Your file should look like the following:
<hibernate-configuration>
<session-factory name="session1">
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sakila</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">######</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
</session-factory>
</hibernate-configuration>
-
Save your changes to the file.
After you create the form and set it as the main class you will be able to see the SQL query printed in the IDE’s Output window when you run the project.
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 Hibernate’s configure()
method, 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 sakila.util as the package name. 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 plain old Java object (POJO), Actor.java
, to represent the data in the table ACTOR in the database. The class specifies the fields for the columns in the tables and uses simple setters and getters to retrieve and write the data. To map Actor.java
to the ACTOR table you can use a Hibernate mapping file or use annotations in the class.
You can use the Reverse Engineering wizard and the Hibernate Mapping Files and POJOs from a Database wizard to create multiple POJOs and mapping files based on database tables that you select. Alternatively, you can use wizards in the IDE to help you create individual POJOs and mapping files from scratch.
Notes.
-
When you want to create files for multiple tables you will most likely want to use the wizards. In this tutorial you only need to create one POJO and one mapping file so it is fairly easy to create the files individually. You can see the steps for creating the POJOs and mapping files individually at the end of this tutorial.
Creating the Reverse Engineering File
The reverse engineering file ( hibernate.reveng.xml
) is an XML file that can be used to modify the default settings used when generating Hibernate files from the metadata of the database specified in hibernate.cfg.xml
. The wizard generates the file with basic default settings. You can modify the file to explicitly specify the database schema that is used, to filter out tables that should not be used and to specify how JDBC types are mapped to Hibernate types.
-
Right-click the Source Packages node and select New > Other to open the New File wizard.
-
Select Hibernate from the Categories list and Hibernate Reverse Engineering Wizard from the File Types list. Click Next.
-
Type hibernate.reveng for the file name.
-
Keep the default *
src
* as the Location. Click Next. -
Select actor in the Available Tables pane and click Add. Click Finish.
The wizard generates a hibernate.reveng.xml
reverse engineering file. You can close the reverse engineering file because you will not need to edit the file.
Creating Hibernate Mapping Files and POJOs From a Database
The Hibernate Mapping Files and POJOs from a Database wizard generates files based on tables in a database. When you use the wizard, the IDE generates POJOs and mapping files for you based on the database tables specified in hibernate.reveng.xml
and then 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).
-
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.
-
Select
hibernate.cfg.xml
from the Hibernate Configuration File dropdown list, if not selected. -
Select
hibernate.reveng.xml
from the Hibernate Reverse Engineering File dropdown list, if not selected. -
Ensure that the Domain Code and Hibernate XML Mappings options are selected.
-
Type sakila.entity for the Package name. Click Finish.
When you click Finish, the IDE generates the POJO Actor.java
with all the required fields and generates a Hibernate mapping file and adds the mapping entry to hibernate.cfg.xml
.
Now that you have the POJO and necessary Hibernate-related files you can create a simple Java GUI front end for the application. You will also create and then add an HQL query that queries the database to retrieve the data. In this process we also use the HQL editor to build and test the query.
Creating the Application GUI
In this exercise you will create a simple JFrame Form with some fields for entering and displaying data. You will also add a button that will trigger a database query to retrieve the data.
If you are not familiar with using the GUI builder to create forms, you might want to review the Introduction to GUI Building tutorial.
Creating the JFrame Form
-
Right-click the project node in the Projects window and choose New > Other to open the New File wizard.
-
Select JFrame Form from the Swing GUI Forms category. Click Next.
-
Type DVDStoreAdmin for the Class Name and type sakila.ui for the Package. Click Finish.
When you click Finish the IDE creates the class and opens the JFrame Form in the Design view of the editor.
Adding Elements to the Form
You now need to add the UI elements to the form. When the form is open in Design view in the editor, the Palette appears in the left side of the IDE. To add an element to the form, drag the element from the Palette into the form area. After you add an element to the form you need to modify the default value of the Variable Name property for that element.
-
Drag a Label element from the Palette and change the text to Actor Profile.
-
Drag a Label element from the Palette and change the text to First Name.
-
Drag a Text Field element next to the First Name label and delete the default text.
-
Drag a Label element from the Palette and change the text to Last Name.
-
Drag a Text Field element next to the Last Name label and delete the default text.
-
Drag a Button element from the Palette and change the text to Query.
-
Drag a Table element from the Palette into the form.
-
Modify the Variable Name values of the following UI elements according to the values in the following table.
You can modify the Variable Name value of an element by right-clicking the element in the Design view and then choosing Change Variable Name. Alternatively, you can change the Variable Name directly in the Inspector window.
You do not need to assign Variable Name values to the Label elements.
Element | Variable Name |
---|---|
First Name text field |
|
Last Name text field |
|
Query button |
|
Table |
|
-
Save your changes.
In Design view your form should look similar to the following image.
Now that you have a form you need to create the code to assign events to the form elements. In the next exercise you will construct queries based on Hibernate Query Language to retrieve data. After you construct the queries you will add methods to the form to invoke the appropriate query when the Query button is pressed.
Creating the Query in the HQL Query Editor
In the IDE you can construct and test queries based on the Hibernate Query Language (HQL) using the HQL Query Editor. As you type the query the editor shows the equivalent (translated) SQL query. When you click the 'Run HQL Query' button in the toolbar, the IDE executes the query and shows the results at the bottom of editor.
In this exercise you use the HQL Editor to construct simple HQL queries that retrieve a list of actors' details based on matching the first name or last name. Before you add the query to the class you will use the HQL Query Editor to test that the connection is working correctly and that the query produces the desired results. Before you can run the query you first need to compile the application.
-
Right-click the project node and choose Build.
-
Expand the <default package> source package node in the Projects window.
-
Right-click
hibernate.cfg.xml
and choose Run HQL Query to open the HQL Editor. -
Test the connection by typing
from Actor
in the HQL Query Editor. Click the Run HQL Query button ( ) in the toolbar.
When you click Run HQL Query you should see the query results in the bottom pane of the HQL Query Editor.
-
Type the following query in the HQL Query Editor and click Run HQL Query to check the query results when the search string is 'PE'.
from Actor a where a.firstName like 'PE%'
The query returns a list of actors' details for those actors whose first names begin with 'PE'.
If you click the SQL button above the results you should see the following equivalent SQL query.
select actor0_.actor_id as col_0_0_ from sakila.actor actor0_ where (actor0_.first_name like 'PE%' )
-
Open a new HQL Query Editor tab and type the following query in the editor pane. Click Run HQL Query.
from Actor a where a.lastName like 'MO%'
The query returns a list of actors' details for those actors whose last names begin with 'MO'.
Testing the queries shows that the queries return the desired results. The next step is to implement the queries in the application so that the appropriate query is invoked by clicking the Query button in the form.
Adding the Query to the Form
You now need to modify DVDStoreAdmin.java
to add the query strings and create the methods to construct and invoke a query that incorporates the input variables. You also need to modify the button event handler to invoke the correct query and add a method to display the query results in the table.
-
Open
DVDStoreAdmin.java
and click the Source tab. -
Add the following query strings (in bold) to the class.
public DVDStoreAdmin() {
initComponents();
}
*private static String QUERY_BASED_ON_FIRST_NAME="from Actor a where a.firstName like '";
private static String QUERY_BASED_ON_LAST_NAME="from Actor a where a.lastName like '";*
It is possible to copy the queries from the HQL Query Editor tabs into the file and then modify the code.
-
Add the following methods to create the query based on the user input string.
private void runQueryBasedOnFirstName() {
executeHQLQuery(QUERY_BASED_ON_FIRST_NAME + firstNameTextField.getText() + "%'");
}
private void runQueryBasedOnLastName() {
executeHQLQuery(QUERY_BASED_ON_LAST_NAME + lastNameTextField.getText() + "%'");
}
The methods call a method called executeHQLQuery()
and create the query by combining the query string with the user entered search string.
-
Add the
executeHQLQuery()
method.
private void executeHQLQuery(String hql) {
try {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Query q = session.createQuery(hql);
List resultList = q.list();
displayResult(resultList);
session.getTransaction().commit();
} catch (HibernateException he) {
he.printStackTrace();
}
}
The executeHQLQuery()
method calls Hibernate to execute the selected query. This method makes use of the HibernateUtil.java
utility class to obtain the Hibernate Session.
-
Right-click in the editor and choose Fix Imports (Ctrl-Shift-I; ⌘-Shift-I on Mac) to generate import statements for the Hibernate libraries (
org.hibernate.Query
,org.hibernate.Session
) andjava.util.List
. Save your changes. -
Create a Query button event handler by switching to the Design view and double-clicking the Query button.
The IDE creates the queryButtonActionPerformed
method and displays the method in the Source view.
-
Modify the
queryButtonActionPerformed
method in the Source view by adding the following code so that a query is run when the user clicks the button.
private void queryButtonActionPerformed(java.awt.event.ActionEvent evt) {
*if(!firstNameTextField.getText().trim().equals("")) {
runQueryBasedOnFirstName();
} else if(!lastNameTextField.getText().trim().equals("")) {
runQueryBasedOnLastName();
}*
}
-
Add the following method to display the results in the JTable.
private void displayResult(List resultList) {
Vector<String> tableHeaders = new Vector<String>();
Vector tableData = new Vector();
tableHeaders.add("ActorId");
tableHeaders.add("FirstName");
tableHeaders.add("LastName");
tableHeaders.add("LastUpdated");
for(Object o : resultList) {
Actor actor = (Actor)o;
Vector<Object> oneRow = new Vector<Object>();
oneRow.add(actor.getActorId());
oneRow.add(actor.getFirstName());
oneRow.add(actor.getLastName());
oneRow.add(actor.getLastUpdate());
tableData.add(oneRow);
}
resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
}
-
Right-click in the editor and choose Fix Imports (Ctrl-Shift-I; ⌘-Shift-I on Mac) to generate an import statement for
java.util.Vector
andjava.util.List
. Save your changes.
After you save the form you can run the project.
Running the Project
Now that the coding is finished, you can launch the application. Before you run the project, you need to specify the application’s Main Class in the project’s properties dialog box. If no Main Class is specified, you are prompted to set it the first time that you run the application.
-
Right-click the project node in the Projects window and choose Properties.
-
Select the Run category in the Project Properties dialog box.
-
Type sakila.ui.DVDStoreAdmin for the Main Class. Click OK.
Alternatively, you can click the Browse button and choose the main class in the dialog box.
-
Click Run Project in the main toolbar to launch the application.
Type in a search string in the First Name or Last Name text field and click Query to search for an actor and see the details.
If you look in the Output window of the IDE you can see the SQL query that retrieved the displayed results.
Downloading the Solution Project
You can download the solution to this tutorial as a project in the following ways.
-
Download a zip archive of the finished project.
-
Checkout the project sources from the NetBeans Samples by performing the following steps:
-
Choose Team > Subversion > Checkout from the main menu.
-
In the Checkout dialog box, enter the following Repository URL:
https://svn.netbeans.org/svn/samples~samples-source-code
Click Next.-
Click Browse to open the Browse Repostiory Folders dialog box.
-
Expand the root node and select samples/java/DVDStoreAdmin-Ant. Click OK.
-
Specify the Local Folder for the sources (the local folder must be empty).
-
Click Finish.
-
-
When you click Finish, the IDE initializes the local folder as a Subversion repository and checks out the project sources.
-
Click Open Project in the dialog that appears when checkout is complete.
Note. You need a Subversion client to checkout the sources. For more about installing Subversion, see the section on Setting up Subversion in the Guide to Subversion in NetBeans IDE.
Creating POJOs and Mapping Files Individually
Because a POJO is a simple Java class you can use the New Java Class wizard to create the class and then edit the class in the source editor to add the necessary fields and getters and setters. After you create the POJO you then use a wizard to create a Hibernate mapping file to map the class to the table and add mapping information to hibernate.cfg.xml
. When you create a mapping file from scratch you need to map the fields to the columns in the XML editor.
Note. This exercise is optional and describes how to create the POJO and mapping file that you created with the Hibernate Mapping Files and POJOs from Database wizard.
-
Right-click the Source Packages node in the Projects window and choose New > Java Class to open the New Java Class wizard.
-
In the wizard, type Actor for the class name and type sakila.entity for the package. Click Finish.
-
Make the following changes (displayed in bold) to the class to implement the Serializable interface and add fields for the table columns.
public class Actor *implements Serializable* {
*private Short actorId;
private String firstName;
private String lastName;
private Date lastUpdate;*
}
-
Right-click in the editor and choose Insert Code (Alt-Insert; Ctrl-I on Mac) and select Getter and Setter in the popup menu to generate getters and setters for the fields.
-
In the Generate Getters and Setters dialog box, select all the fields and click Generate.
In the Generate Getters and Setters dialog box, you can use the Up arrow on the keyboard to move the selected item to the Actor node and then press the Space bar to select all fields in Actor.
-
Fix your imports and save your changes.
After you create the POJO for the table you will want to create an Hibernate Mapping File for Actor.java
.
-
Right-click the
sakila.entity
source packages node in the Projects window and choose New > Other to open the New File wizard. -
Select Hibernate Mapping Wizard in the Hibernate category. Click Next.
-
Type Actor.hbm for the File Name and check that the Folder is src/sakila/entity. Click Next.
-
Type sakila.entity.Actor for the Class to Map and select actor from the Database Table drop down list. Click Finish.
When you click Finish the Actor.hbm.xml
Hibernate mapping file opens in the source editor. The IDE also automatically adds an entry for the mapping resource to hibernate.cfg.xml
. You can view the entry details by expanding the Mapping node in the Design view of hibernate.cfg.xml
or in the XML view. The mapping
entry in the XML view will look like the following:
<mapping resource="sakila/entity/Actor.hbm.xml"/>
</session-factory>
</hibernate-configuration>
-
Map the fields in
Actor.java
to the columns in the ACTOR table by making the following changes (in bold) toActor.hbm.xml
.
<hibernate-mapping>
<class name="sakila.entity.Actor" *table="actor">
<id name="actorId" type="java.lang.Short">
<column name="actor_id"/>
<generator class="identity"/>
</id>
<property name="firstName" type="string">
<column length="45" name="first_name" not-null="true"/>
</property>
<property name="lastName" type="string">
<column length="45" name="last_name" not-null="true"/>
</property>
<property name="lastUpdate" type="timestamp">
<column length="19" name="last_update" not-null="true"/>
</property>
</class>*
</hibernate-mapping>
You can use code completion in the editor to complete the values when modifying the mapping file.
By default, the generated class element has a closing tag. Because you need to add property elements between the opening and closing class element tags, you need to make the following changes (displayed in bold). After making the changes you can then use code completion between the class tags.
|
<hibernate-mapping>
<class name="sakila.entity.Actor" *table="actor">
</class>*
</hibernate-mapping>
-
Click the Validate XML button in the toolbar and save your changes.
Creating individual POJOs and Hibernate mapping files might be a convenient way to further customizing your application.
See Also
For additional information on creating Swing GUI applications, see the following tutorials.