June 18, 2019

DAO Factory patterns with Hibernate

Hibernate is a OR-mapped Java persistence framework which not only takes care of the mapping from Java classes to database tables, but also provides data query and retrieval facilities. In a previous article, I’ve explained how to¬†integrate a Hibernate minimal configuration in a Struts MVC web application.
Now I will show how to add an advanced Hibernate persistence implementation using the DAO and Factory design patterns from the CaveatEmptor example application. This solution combines both the Factory and the DAO design patterns:

Factory pattern, creates object without exposing the creation logic to the client and refers to newly created object using a common interface.
Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services .

This approach provides higher abstraction to the persistence layer of a web application. Here a high level class diagram.


SOURCE CODE (/giuseu/struts-mvc)

git clone https://gitlab.com/giuseppeurso-eu/struts-mvc

Let’s start with the DAO pattern. We add the abstract factory and the concrete factory for Hibernate DAOs.

– DAOFactory


– DAOHibernateFactory

We continue with the GenericDAO interface and its implementation for common CRUD functionality

– GenericDAO

– GenericHibernateDAO

Here is the project layout


Related posts

Leave a Reply

Your email address will not be published.