Over a million developers have joined DZone.

How to Use Hibernate in a NetBeans Platform Application

DZone's Guide to

How to Use Hibernate in a NetBeans Platform Application

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

In this tutorial I show you how to combine a NetBeans Platform application with Hibernate. For this simple getting started example, I use the basic Hibernate features.

This tutorial is based on the Patient Administration application built with Geertjan during the NetBeans Platform Certified training session in Burkina Faso. The application is very basic. It enables the insertion and deletion of a patient, while it also enables the retrieval of the list of patients for display in an explorer window:

Create a Database

First of all you need to create a database “mydatabase as shown in the picture below. As a sample database, there is no need for any password for the administrator:

Secondly, create a table named "patient", using the script below:

Create a NetBeans Platform Application

As stated above, the tutorial is based on an application built during a NetBeans Platform training session. There are three main modules: ModelPatient, PatientView, and PatientWindows. The fourth, hibernate, is a wrapper library module.

ModelPatient contains the POJO class, mapping files, and Hibernate configuration file:

The POJO class is here named "Patient.java". You can look here at a listing of the source code:

You must create a mapping file, as follows:

In order to allow mapping file to access the Patient entity, you must specify the absolute path of the Patient class in the mapping file above, as follows:

<class name="org.netbeans.modelpatient.Patient" table="patient">

An important file to be created is the Hibernate configuration file. I named it hibernate.cfg.xml:

In order to allow the Hibernate configuration file to access the mapping resource, you must specify the absolute path of the mapping file. In my example, above, the mapping file is located in package org.netbeans.patientmodel. Then in the configuration file I have the following:

<mapping resource="org/netbeans/modelpatient/Patient.hbm.xml"/>

HibernateUtil.java is a standard Java class that lets you perform persistence operations. A session factory is important for Hibernate. It implements a design pattern that ensures that only one instance of the session is used per thread. You should only get your Hibernate session from this factory:

This part of the code is extremely important. When the Hibernate configuration file is not on the CLASSPATH you must specify explicitly its path, as shown above:

    static {
URL myurl = Thread.currentThread().getContextClassLoader().getResource("org/netbeans/modelpatient/hibernate.cfg.xml");
sessionFactory = new Configuration().configure(myurl).buildSessionFactory();

To configure Log4J, create a file named log4j.properties in the root directory and insert the following:

The wrapper library module "hibernate" contains the Hibernate and MySQL libraries:

PatientWindows provides a window with two buttons, Save and Cancel:

Here is the source code for the Save button:

Under the Cancel button, add this code to quit the application:

The APIs necessary for this module are:


In the PatientView module, the modification you need to make is to dynamically set the list of patients from the database to the node. Here the "createKeys" method in the node is used as follows:


The APIs needed in this module are as follows:


It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}