2007-10-18

Generics makes DAOs easy

We've all been there. You're developing a medium-to-large application that uses a POJO persistence engine such as Hibernate. Good software design advocates that you have a DAO for each type of POJO, so off you go writing some. But after five–ten iterations of writing CRUD methods, it gets boring, doesn't it?

Here's how to make it easy.


Assumptions


This article uses generics; generics are supported starting with Java 5. My examples use classes from the Spring framework and assume that you're using Hibernate as your persistence engine, but the techniques illustrated should be applicable to other setups.

The interface

The standard DAO

Every DAO should support the basic CRUD methods:
  • Create
  • Retrieve
  • Update
  • Destroy
I tend to have three methods:

save
Saves an object, updating if it is already present; this handles C and U.

get
Fetches an object by id; this handles R.

remove
Deletes an object from the persistent store; this handles D.

In addition, I like to define methods to remove an object by its id, load all objects of the type, and to reassociate an object with the persistence layer. This last method is useful in an MVC environment, where your persistent object may make several round-trips to the view layer before you're done. So we end up with the following interface:
 public interface BaseDAO {
void save(Object o);

Object get(Serializable id);

void remove(Object o);

void remove(Serializable id);

List loadAll();

void reassociate(Object o);
}

The generic DAO interface, version 1

Adding generics

Notice all the Object references in that interface? That's not very modern, and it means that we have to litter our code with potentially unsafe casts. Let's add some generics.
 public interface BaseDAO<T> {
void save(T o);

T get(Serializable id);

void remove(T o);

void remove(Serializable id);

List<T> loadAll();

void reassociate(T o);
}

The generic DAO interface, version 2

A concrete DAO

With this interface defined, we can define a concrete DAO interface for the fictitious Order class:

 public interface OrderDAO extends BaseDAO<Order> { }

The OrderDAO interface

Yes, that is all there is to it. This interface now has methods to do CRUD operations on Order objects, with compile-time type safety.

The implementation

Implementing the generic DAO

The implementation of the generic DAO is straightforward:

 public abstract class BaseDAOHibernate<T>
extends HibernateDaoSupport
implements BaseDAO<T> {
public void save(T object) {
getHibernateTemplate().saveOrUpdate(object);
}

public T get(Serializable id) {
return getModelClass().cast(getHibernateTemplate().get(getModelClass(), id));
}

public void remove(T object) {
getHibernateTemplate().delete(object);
}

public void remove(Serializable id) {
remove(get(id));
}

@SuppressWarnings("unchecked")
public List<T> loadAll() {
return getHibernateTemplate().loadAll(getModelClass());
}

public void reassociate(T object) {
getHibernateTemplate().lock(object, LockMode.NONE);
}

protected abstract Class<T> getModelClass();
}

Implementing BaseDAO

I added the @SuppressWarnings annotation to get rid of a compiler warning about an unchecked cast.

Implementing OrderDAO

Now comes the reason for doing all this:

 public class OrderDAOHibernate
extends BaseDAOHibernate<Order>
implements OrderDAO {
protected Class<Order> getModelClass() {
return Order.class;
}
}


Implementing OrderDAO

And there you have it, a complete DAO for Order objects in seven neatly-formatted lines of code.

Conclusion

Generics can be a difficult subject to grasp, but when the benefits are as major as shown here, it is well worth the effort to get to grips with it.

Writing less code is always a big win. Not only is there less work to do (meaning that you get to go home early), but it is easier to debug the code that is present.

8 comments:

Unknown said...

Really very great information for that post, am amazed and then more new information are get after refer that post. I like that post.
Java Training in Chennai | Java Training Institute in Chennai

Faizal said...

Excellent works!!!Information's are amazing visit here for more...
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

James Williams said...

Nice Blog, I hope gather more information here. thanks for sharing the informative information.
Java Online Training
Java Online Training In Chennai
Core Java Online Training

jenani said...

I am really enjoying reading your well written articles.
Java Training in Chennai

Java Training in Velachery

Java Training in Tambaram

Java Training in Porur

Java Training in Omr

Java Training in Annanagar

vanathi said...

I have read your blog its very attractive and impressive. I like it your blog.keep posting like this and thank for share with us
Software Testing Training in Chennai

Software Testing Training in Velachery

Software Testing Training in Tambaram

Software Testing Training in Porur

Software Testing Training in Omr
Software Testing Training in Annanagar


subathara said...

I have read your blog its very attractive and impressive. I like it your blog.
Digital Marketing Training in Chennai

Digital Marketing Training in Velachery

Digital Marketing Training in Tambaram

Digital Marketing Training in Porur

Digital Marketing Training in Omr

Digital MarketingTraining in Annanagar

cyber pc said...

i found your this call despite the fact that searching for a few related reference concerning blog search...Its a nice publicize..store posting and update the mention. Nord VPN Crack

softkeybox said...

Thanks for making this superb blog for us.
Anytrans Torrent

Driver Booster 9.2 License Key

Freemake Video Converter Key

Vsdc Pro Crack