Tag Archives: Data Access

Hibernate JPA DAO

Hibernate JPA DAO

This is an example of how to create Data Access Objects (DAOs), making use of the Hibernate implementation for the Java Persistence API (JPA) specification. Hibernateis an object-relational mapping library for Java, that provides a framework for mapping an object-oriented domain model to a traditional relational database.

When an application interacts with a database, it is common pattern to separate all the low level data access operations from high level business services. This can be achieved using DAOs, which are objects that provide abstract interfaces to the database. DAOs may be used from services in higher layers of the application, thus connecting a service layer and the database.

Below, we will create a DAO class (Data Access Layer) and will call its interface in another class (Service Layer) to create, read, update and delete rows (CRUD operations) from a database table.

Tip: You may skip project creation and jump directly to the beginning of the example below.

Our preferred development environment is Eclipse. We are using Eclipse Juno (4.2) version, along with Maven Integration plugin version 3.1.0. You can download Eclipse from here and Maven Plugin for Eclipse from here. The installation of Maven plugin for Eclipse is out of the scope of this tutorial and will not be discussed. We are also using the JDK 7_u_21. The Hibernate version is 4.3.6, and the database used in the example is MySQL Database Server 5.6.

Let’s begin,