Thursday, February 19, 2009

A neat little DAO for hibernate I came up with

No need to create new DAOs for each project if you use this little gem. Its not in groovy because GMaven has some problems with generics but this has seemed to remove the need to have multiple DAOs for annotated classes that just do basic crud functionality. Check it out and feel free to comment(Not that anyone reads these anyway :-)).

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;

public class GenericDao extends HibernateDaoSupport{
@SuppressWarnings("unchecked")
public List getObject(T object){
return getHibernateTemplate().findByExample(object);
}
@SuppressWarnings("unchecked")
public List getAllObjects(Class thisClass){
return getHibernateTemplate().loadAll(thisClass);
}
@SuppressWarnings("unchecked")
public Object addObject(T object){
return getHibernateTemplate().save(object);
}
@SuppressWarnings("unchecked")
public void updateObject(T object){
getHibernateTemplate().update(object);
}
}

No comments: