www.pudn.com > SSPP.rar > TTeacherDAO.java


package edu.neu.sspp.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; 
 
/** 
 * Data access object (DAO) for domain model class TTeacher. 
 * @see edu.neu.sspp.hibernate.TTeacher 
 * @author MyEclipse - Hibernate Tools 
 */ 
public class TTeacherDAO extends BaseHibernateDAO { 
 
    private static final Log log = LogFactory.getLog(TTeacherDAO.class); 
 
	//property constants 
	public static final String NAME = "name"; 
	public static final String PASSWORD = "password"; 
	public static final String REAL_NAME = "realName"; 
	public static final String EMAIL = "email"; 
	public static final String STUDENTS = "students"; 
 
     
    public void save(TTeacher transientInstance) { 
        log.debug("saving TTeacher instance"); 
        try { 
            getSession().save(transientInstance); 
            log.debug("save successful"); 
        } catch (RuntimeException re) { 
            log.error("save failed", re); 
            throw re; 
        } 
    } 
     
	public void delete(TTeacher persistentInstance) { 
        log.debug("deleting TTeacher instance"); 
        try { 
            getSession().delete(persistentInstance); 
            log.debug("delete successful"); 
        } catch (RuntimeException re) { 
            log.error("delete failed", re); 
            throw re; 
        } 
    } 
     
    public TTeacher findById( java.lang.String id) { 
        log.debug("getting TTeacher instance with id: " + id); 
        try { 
            TTeacher instance = (TTeacher) getSession() 
                    .get("edu.neu.sspp.hibernate.TTeacher", id); 
            return instance; 
        } catch (RuntimeException re) { 
            log.error("get failed", re); 
            throw re; 
        } 
    } 
     
    public int getCount() { 
    	log.debug("getting count of TTeacher"); 
    	 
    	try { 
        	String queryString = "select count(*) from TTeacher"; 
        	Query queryObject = getSession().createQuery(queryString); 
        	 
        	return ((Integer)queryObject.list().get(0)).intValue(); 
    	} catch (RuntimeException re) { 
    		log.error("getting count of TTeacher failed", re); 
    		throw re; 
    	} 
    } 
     
    public List getByPage(int first, int size) { 
    	log.debug("getting TTeacher for page"); 
    	 
    	try { 
    		//按用户名排列 
        	String queryString = "from TTeacher as teacher order by teacher.name"; 
        	Query queryObject = getSession().createQuery(queryString); 
        	queryObject.setFirstResult(first); 
        	//mysql似乎不支持setFetchSize,无法实现部分查询,只能通过setMaxResults在返回结果上控制 
        	//queryObject.setFetchSize(size); 
        	queryObject.setMaxResults(size); 
        	return queryObject.list(); 
    	} catch(RuntimeException re) { 
    		log.error("get by page failed", re); 
    		throw re; 
    	} 
    } 
     
    public List getAll() { 
    	log.debug("getting all teachers"); 
    	 
    	try { 
        	String queryString = "from TTeacher"; 
        	Query queryObject = getSession().createQuery(queryString); 
        	 
        	return queryObject.list(); 
    	} catch(RuntimeException re) { 
    		log.error("get all teachers failed", re); 
    		throw re; 
    	} 
    } 
     
    public List findByExample(TTeacher instance) { 
        log.debug("finding TTeacher instance by example"); 
        try { 
            List results = getSession() 
                    .createCriteria("edu.neu.sspp.hibernate.TTeacher") 
                    .add(Example.create(instance)) 
            .list(); 
            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 TTeacher instance with property: " + propertyName 
            + ", value: " + value); 
      try { 
         String queryString = "from TTeacher as model where model."  
         						+ propertyName + "= ?"; 
         Query queryObject = getSession().createQuery(queryString); 
		 queryObject.setParameter(0, value); 
		 return queryObject.list(); 
      } catch (RuntimeException re) { 
         log.error("find by property name failed", re); 
         throw re; 
      } 
	} 
 
	public List findByName(Object name) { 
		return findByProperty(NAME, name); 
	} 
	 
	public List findByPassword(Object password) { 
		return findByProperty(PASSWORD, password); 
	} 
	 
	public List findByRealName(Object realName) { 
		return findByProperty(REAL_NAME, realName); 
	} 
	 
	public List findByEmail(Object email) { 
		return findByProperty(EMAIL, email); 
	} 
	 
	public List findByStudents(Object students) { 
		return findByProperty(STUDENTS, students); 
	} 
	 
    public TTeacher merge(TTeacher detachedInstance) { 
        log.debug("merging TTeacher instance"); 
        try { 
            TTeacher result = (TTeacher) getSession() 
                    .merge(detachedInstance); 
            log.debug("merge successful"); 
            return result; 
        } catch (RuntimeException re) { 
            log.error("merge failed", re); 
            throw re; 
        } 
    } 
 
    public void attachDirty(TTeacher instance) { 
        log.debug("attaching dirty TTeacher instance"); 
        try { 
            getSession().saveOrUpdate(instance); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
     
    public void attachClean(TTeacher instance) { 
        log.debug("attaching clean TTeacher instance"); 
        try { 
            getSession().lock(instance, LockMode.NONE); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
}