Generic CRUD DAO layer

The last few days I read about generic in java. I knew a little bit, again, I wanted to find out a bit more. So I started to dig on generics and the end of the day my feeling was’ wow, this is just amazing! “I think you may have the same feelings, if you learn a little about generics. In this article, I am not going to write a tutorial on generic and but want to show you what I have played with a generic. I wrote a generic DAO layer using Hibernate.

Let me go more details what I have written in last days.

The main Classes and interfaces that I have written:

BEntity: base entity class. It’s an abstract class. Every entity must have to extend this class otherwise this generic dao won’t work.

IDeleteable :  an empty interface. It is used for soft delete separation purpose. In this generic dao layer I wanted to achieve some goals, I will discuss them later.

DEntity: it’s another class which extends BEntity and implements IDeleteable interface. If we want to delete an entity permanently from database, we will extends this class otherwise

ICrudDao<T extends BEntity>:  basic crud interface. A lot of common method for crud operation is here.

CrudDaoImpl<T extends BEntity > implements ICrudDao:  implementation of ICrudDao interface.

The common methods of ICrudDao:

I have all this method’s implementation in my code.

I have some goals to write this generic crud dao layer.


  1. I wanted to make sure that every entity will extend the BEntity or DEntity. BEntity stands for base entity. And DEntity stands for deleteable entity which means this entity can be deleted. If any entity extends BEntity, which means this entity won’t be deleted permanently but will be performed a soft delete operation which means a Boolean flag will be changed and this entity will not be visible anymore.
  2. There are some common property in the BEntity, id, creationDate, lastUpdatedDate, version and deleted. We know every entity needs to have an id. It’s common for every entity.  And here I want to store some common property to track an entity like when it is created, when it is last deleted. So creationDate and lastUpdateDate are used for that purpose. And the property version is used for optimistic locking. What is optimistic locking, I will tell you later. And the ‘deleted’ property is a Boolean flag. It is used for soft delete purpose. If the entity is not deletable, we’ll basically change the visibility of the entity.
  3. And I wanted to generalize the common operation of CRUD. We know every entity has some common operation, like create, delete, update, store, fetch all, count, find etc. The main goal of this generic dao is generalize this CRUD operation. We won’t write code for every time for the same type of operation. This is how we can achieve “Don’t Repeat Yourself (DRY)” pattern.

If you want to use my generic dao layer, just clone or fork from github


If you want to use this generic dao layer, life is simple. If you want to have deletable entity, extend DEntity, otherwise extend BEntity.

Say I have a User entity and additionally I have two more property and they are username and password.

package org.codexplo.cruder.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

* Example entity for testing purpose
* @author Rokonoid

@Table(name = "User")
public class User extends BEntity {

private static final long serialVersionUID = -207732145432694522L;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;

public String getName() {
return name;

public String getPassword() {
return password;

public void setName(String name) { = name;

public void setPassword(String password) {
this.password = password;

I  extended BEntity as I don’t want to delete my entity permanently.

Now my IUserDao interface will be so simple.

package org.codexplo.cruder.dao;

import org.codexplo.cruder.domain.User;

public interface IUserDao extends ICrudDao {


See, I have no method in this interface. I can use this interface for all the crud operations. And if I need additional methods I may add those methods here.

And my UserDaoImpl

package org.codexplo.cruder.dao;

import org.codexplo.cruder.domain.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

public class UserDaoImpl extends CrudDaoImpl implements IUserDao {


It is also empty.

So this generic crud dao will help you to reduce a lot of code to be written.

Click this image to fork github


1 Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s