public class CommonDao extends HibernateDaoSupport {
public void addObject(Object obj){
this.getHibernateTemplate().save(obj);
}

public void deleObject(Class<?> clazz,int id){
this.getHibernateTemplate().delete(findObjectById(clazz,id));
}

public void updateObject(Object obj){
this.getHibernateTemplate().update(obj);
}

public Object findObjectById(Class<?> clazz,int id){
return this.getHibernateTemplate().get(clazz, id);
}

@SuppressWarnings("unchecked")
public List<Object> getList(Class<?> clazz){
return this.getHibernateTemplate().find("from "+clazz.getSimpleName()+" order by id desc");
} @SuppressWarnings("unchecked")
public List<Object> getList(Class<?> clazz,String sql){
return this.getHibernateTemplate().find("from "+clazz.getSimpleName()+" where 1=1 "+sql+" order by id desc");
}

public Long getObjectCount(Class<?> clazz){
return (Long)this.getHibernateTemplate().find("select count(*) from "+clazz.getSimpleName()).get(0);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Object> getObjectList(final Class<?> clazz,final int pn,final int ps){
return (List<Object>)this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery("from "+clazz.getSimpleName()+" order by id desc")
.setFirstResult((pn-1)*ps)
.setMaxResults(ps)
.list();
}
});
}


public Long getObjectCount(final Class<?> clazz,String sql){
return (Long)this.getHibernateTemplate().find("select count(*) from "+clazz.getSimpleName()+" where 1=1 "+sql).get(0);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Object> getObjectList(final Class<?> clazz,final String sql,final int pn,final int ps){
return (List<Object>)this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery("from "+clazz.getSimpleName()+" where 1=1 "+sql+" order by id desc")
.setFirstResult((pn-1)*ps)
.setMaxResults(ps)
.list();
}
});
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Object> getSqlList(final String sql){
return (List<Object>)this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createSQLQuery(sql).list();
}
});
}
}