A Generic DAO for Hibernate

This short blog shares one Generic DAO (Data Access Object) for Hibernate that may be useful in a Spring 3 project. The source code is available for download at the bottom of this page.

  Note: It requires commons-beansutils-1.8.3.jar

Usage Example

Let’s assume a MySQL table as so:


A matching hibernate Entity such as this:


The key now is to extend Dao.java with the concrete type. In this example TestDao.java will be an appropriate subclass.

 Note that this TestDao class is a Spring Dao. This generic DAO is ideal for Spring as the transaction control is delegated to Spring.

From this, listing, pagination, and filtering is made effortless.

Note that ordering (where applicable) occurs before pagination as it should.

A class using the Dao may look like this:

 Again this is a Spring service with annotated transaction control

Here’s some SQL useful for testing it


Or, try it out using this Netbeans project. The project is a Spring Web MVC project.

The DB is a MySQL DB and is called ‘test’. The SQL code is in the WEB-INF folder.

There will be two references that will need to be resolved. Get the jar files here and add them to you’r class path:

1) commons-beansutils-1.8.3.jar

2) aopalliance-1.0.jar

Netbeans project download: GenericDAOTest.zip

Comments always welcome! Thanks.