Integrating Database Layer – Shop Smart


In the previous post Spring Application Setup – Shop Smart we learnt how to setup Spring and JUnit for development environment using Eclipse.

Let us now add a data access layer (commonly known as DAO layer) to our application, so that we can easily communicate with the database (HSQL or MySQL or any other database supported by Hibernate).

Hibernate – A brief Discussion

Hibernate is a huge topic and deserves its own series of posts. Here I would just like to point out few basic stuff about this framework. It all starts with the need of persisting an object.

Imagine that a user is interacting with our application on a daily basis. He logs in everyday, updates a lot of things and then logs out. The next day he logs in, he would expect to see the changes he made in the last session. So, there is a need of persisting (saving) the data of the user.

The Java Persistence API

Sun (now Oracle) came up with a specification called JPA (Java Persistence API). As per the specification an object in the memory can be related to a table (or group of tables) in the database. Any operation done on the object can be propagated to the database without writing explicit SQL queries.

The whole fuss is about representing the relational database tables in form of Java objects (POJOs). Hence, this framework is also called Object Relational Mapping (ORM).

Similarly any row (or projection in the database tables) can be fetched from the DB and a related object can be populated with the row. There is a lot to say about JPA, I reserve it for some other post.

Hibernate Basics

Hibernate (from JBoss) is an ORM which is an implementation of JPA, I would say most widely used implementation. Other well known implementations are IBatis(Originally Apache) and OpenJPA(Apache).

So, it would be safe to say that JPA is a specification and Hibernate is one of its implementations, similar to interfaces and implementing classes. We will be using Hibernate for interacting with our databases.

The Session Factory

The first thing we need to learn in Hibernate is SessionFactory. It is a factory which can serve sessions on each request.

SessionFactory again is a specification in JPA, Hibernate implements the SessionFactory as a multi-threaded object which is long lived. It is advisable to create one session factory per database in our application. This precisely means that if we connect to MySQL, Oracle, HSQL, SQL Server in the same application, then we can have four different session factories. Mostly we have only one database, so there will be one SessionFactory per application.


The next thing we need to know is the Session. It is an instance of a database session. When a thread asks for a session, the session factory returns a session and we can use it to perform operations on the database. The session only makes sense within the boundaries of a transaction. This means that any operation we perform using the acquired session will run within a transaction.


A transaction is something like a set of operations which can be considered as atomic (a single operation). If any step in the set fails, everything from the start till the end is rolled back.


Entity is the Java object which is related to the table(s) in the database. For our SS_USER table, we can have a UserEntity and so on. Sometimes we create entities without tables as well (to represent ternary relations and vice-versa) but that is advance topic in Hibernate and we will go through it later.

One thing which I would like to enforce is that our Entities must have all the constraints which are present in the database. For e.g.: Unique, Foreign Key etc.

Integrating Database Layer in Shop Smart

Now that we have some clarity about Hibernate, let us look at some code which helps us in connecting to our database.


The complete code can be downloaded from the github repository. Here is the important part snippet

The above class is the object representation of the SS_USER table. Rest of the things are pretty clear.
@Entity – conveys that the class is a JPA Entity which can be persisted.
@Table – is used to relate this entity with a database table, the name of the table us supplied in the annotation value.
@Id – the annotation is put on a field which is the primary key of the table.
@Generated – the annotation tells that the primary key is auto generated.
@Column – all the fields which map to the respective column in the database table, carries this annotation.
@NotEmtpy – this is to enable not null constraint on the field.


It is a good practice to program to an interface. We need the Database layer code from where we connect to the database. For this purpose, let us declare a UserDAO interface as below:

Also, we need an implementation of the interface which does the actual work.


If you notice, we autowired the SessionFactory so that we can get the singleton instance of this class which is created by spring while building application context.
Later we use the session factory to get a new session and using the session we created a criteria query.

The criteria says that consider UserEntity for this query and add a restriction (where clause) such that the userId is equal to the passed value and list the unique result. The method returns the unique result.

The annotation @Repository is a Spring annotation used to demarcate that this class is a DAO layer class and is a valid Spring bean.

And now we are done with the integration for Hibernate. Let us write the test case to test this method. You can download the test class from github.

If you see the setup method of this test case, it first insert a record in the in memory database. The test method then performs assertions on this data.


We can integrate Hibernate to a Spring application in few simple steps. Here we learned the basics of Hibernate and how to create Entities and DAO layer. The DAO layer is not yet complete, but the integration is.


Here is the link for Shop Smart Github repository

Don’t forget to subscribe to TechieMe to get updates on latest posts.