I came from the world of C++ so my knowledge in Java is not perfect...
Using Hibernate with MyEclipse its create for each POJO also a Dao object.
I thought to create generic DAO object and use it instead of all the specifics objects, sow the samples using templates, but I don't like this option, so I created my own class with few methods so far but looks to work fine.
I'll be happy to get some feedback is this way is good / bad, and if I missed something critical.
The code:
Code:
package test.hibernate;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
public class GenericDao extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(KeywordDAO.class);
public String _type;
public GenericDao(String type){
_type = type;
}
public List findAll() {
log.debug("finding all instances");
try {
String queryString = String.format("FROM %s", _type);
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public void delete(Object persistentInstance) {
log.debug( String.format("deleting %s instance", _type) );
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public void save(Object transientInstance) {
log.debug( String.format("saving %s instance", _type) );
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void attachDirty(Object instance) {
log.debug("attaching dirty instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}