public class TEmpDAO extends HibernateDaoSupport { private static final Logger log = LoggerFactory.getLogger(TEmpDAO.class); // property constants public static final String _ENAME = "EName"; public static final String _ESEX = "ESex";
// 专用查询结果集的 , 使用execiteFind();方法查询 public List getAll() { return this.getHibernateTemplate().executeFind(new HibernateCallback() {
//返回需要对应的对象 public Object doInHibernate(Session session) throws HibernateException, SQLException {
//此时有session ,方便操作,可以使用HQL 查询,也可以使用QBC 查询 List ar = session.createCriteria(TEmp.class).createCriteria("TDept").list(); return ar; } }); }
//使用find方法查询 public List testFind(int id,String name){ return this.getHibernateTemplate().find("From TEmp a inner join fetch a.TDept where a.EId>? and a.EName like ?", new Object[]{id,'%'+name}); }
// 根据对象查 public List findByExample(TEmp instance) { log.debug("finding TEmp instance by example"); try { List results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } }
// 根据属性查 public List findByProperty(String propertyName, Object value) { log.debug("finding TEmp instance with property: " + propertyName + ", value: " + value); try { String queryString = "from TEmp as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } }
// 根据名称 public List findByEName(Object EName) { return findByProperty(_ENAME, EName); }
public List findByESex(Object ESex) { return findByProperty(_ESEX, ESex); }
// 查询所有 public List findAll() { log.debug("finding all TEmp instances"); try { String queryString = "from TEmp a inner join fetch a.TDept"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } }