This blog for Hibernate Generic DAO Project has been moved to WordPress http://hibernategenericdao.wordpress.com/. Bye bye Blogger!

Friday, March 20, 2009

Customizable Generic DAOs (The final word on New DAO Methods)

After much internal debate as to which methods to include in our generic DAOs so as to be simple but also flexible enough for a wide variety of development scenarios, we came up with a different sort of solution. We decided simply to make it easy for developers to use their own DAO interfaces and select what methods they would like for their own project.

The change was introduced in version release 0.3.4. We moved all the heavy code to a DAO base class, BaseDAOImpl, that implements a large number of possible methods with that we thought developers might want. These methods all have protected visibility. Developers can now either use our default thin DAO implementation on top of this or make their own by simply selecting which methods they want to expose and wrapping them with public methods according to their own naming convention. In fact to prove how easy this is, the framework currently includes two separate sets of DAOs, "original" with the same methods from our original DAO and "standard" which has a new and improved (at least we think so) set of methods.

Want to know how to make your own DAOs for the Hibernate Generic DAO Framework?
All you have to do is create a class that extends BaseDAOImpl. Actually you'll need one class for the GenericDAO and one for the GeneralDAO if you want to use both, and you will probably also want to create interfaces for these classes as a standard practice. The easiest way is to follow our examples. Just look at the GenericDAOImpl and GeneralDAOImpl classes in these packages:

com.trg.dao.dao.standard (our standard DAOs)
com.trg.dao.dao.original (our original DAOs)

and also the base class javadoc.

The final word on New DAO Methods
In the mean time, we did have to decide what methods to include in our standard DAO. Since I have already written a couple posts about this, I figure I should also post the final decision. So without further ado...

(See javadocs: GeneralDAO, GenericDAO)

Interface for GeneralDAO:

public interface GeneralDAO {

      public T find(Class type, Serializable id);

      public T[] find(Class type, Serializable... ids);

      public T getReference(Class type, Serializable id);

      public T[] getReferences(Class type, Serializable... ids);

      public boolean save(Object entity);

      public boolean[] save(Object... entities);

      public boolean remove(Object entity);

      public void remove(Object... entities);

      public boolean removeById(Class type, Serializable id);

      public void removeByIds(Class type, Serializable... ids);

      public List findAll(Class type);

      public List search(ISearch search);

      public Object searchUnique(ISearch search);

      public int count(ISearch search);

      public SearchResult searchAndCount(ISearch search);

      public boolean isAttached(Object entity);

      public void refresh(Object... entities);

      public void flush();

}


Interface for GenericDAO:

public interface GenericDAOextends Serializable> {

      public T find(ID id);

      public T[] find(ID... ids);

      public T getReference(ID id);

      public T[] getReferences(ID... ids);

      public boolean save(T entity);

      public boolean[] save(T... entities);

      public boolean remove(T entity);

      public void remove(T... entities);

      public boolean removeById(ID id);

      public void removeByIds(ID... ids);

      public List findAll();

      public List search(ISearch search);

      public T searchUnique(ISearch search);

      public int count(ISearch search);

      public SearchResult searchAndCount(ISearch search);

      public List searchGeneric(ISearch search);

      public Object searchUniqueGeneric(ISearch search);

      public boolean isAttached(T entity);

      public void refresh(T... entities);

      public void flush();

}

No comments:

Post a Comment