Over a million developers have joined DZone.

How to Use Hibernate in a NetBeans Platform Application

· Database Zone

To stay on top of the changing nature of the data connectivity world and to help enterprises navigate these changes, download this whitepaper from Progress Data Direct that explores the results of the 2016 Data Connectivity Outlook survey.

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:

LifecycleManager.getDefault().exit();
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:

 

Turn Data Into a Powerful Asset, Not an Obstacle with Democratize Your Data, a Progress Data Direct whitepaper that explains how to provide data access for your users anywhere, anytime and from any source.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}