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


package edu.neu.sspp.hibernate; 
 
import java.util.List; 
import java.util.Set; 
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 TUser. 
 * @see edu.neu.sspp.hibernate.TUser 
 * @author MyEclipse - Hibernate Tools 
 */ 
public class TUserDAO extends BaseHibernateDAO { 
 
    private static final Log log = LogFactory.getLog(TUserDAO.class); 
 
	//property constants 
	public static final String USER_NAME = "userName"; 
	public static final String PASSWORD = "password"; 
	public static final String NICK = "nick"; 
	public static final String EMAIL = "email"; 
	public static final String IS_OPEN = "isOpen"; 
	public static final String COUNT = "count"; 
	public static final String TEACHERS = "teachers"; 
	public static final String NAME = "name"; 
	public static final String SEX = "sex"; 
	public static final String CLASS_ = "class_"; 
	public static final String TEL = "tel"; 
	public static final String INTRO = "intro"; 
 
     
    public void save(TUser transientInstance) { 
        log.debug("saving TUser instance"); 
        try { 
            getSession().save(transientInstance); 
            log.debug("save successful"); 
        } catch (RuntimeException re) { 
            log.error("save failed", re); 
            throw re; 
        } 
    } 
     
	public void delete(TUser persistentInstance) { 
        log.debug("deleting TUser instance"); 
        try { 
            getSession().delete(persistentInstance); 
            log.debug("delete successful"); 
        } catch (RuntimeException re) { 
            log.error("delete failed", re); 
            throw re; 
        } 
    } 
     
    public TUser findById( java.lang.String id) { 
        log.debug("getting TUser instance with id: " + id); 
        try { 
            TUser instance = (TUser) getSession() 
                    .get("edu.neu.sspp.hibernate.TUser", id); 
            return instance; 
        } catch (RuntimeException re) { 
            log.error("get failed", re); 
            throw re; 
        } 
    } 
     
    public int getCount() { 
    	log.debug("getting count of TUser"); 
    	 
    	try { 
        	String queryString = "select count(*) from TUser"; 
        	Query queryObject = getSession().createQuery(queryString); 
        	 
        	return ((Integer)queryObject.list().get(0)).intValue(); 
    	} catch (RuntimeException re) { 
    		log.error("getting count of TUser failed", re); 
    		throw re; 
    	} 
    } 
     
    public List getByPage(int first, int size) { 
    	log.debug("getting TUser for page"); 
    	 
    	try { 
    		//按用户名排列 
        	String queryString = "from TUser as user order by user.userName"; 
        	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 findByExample(TUser instance) { 
        log.debug("finding TUser instance by example"); 
        try { 
            List results = getSession() 
                    .createCriteria("edu.neu.sspp.hibernate.TUser") 
                    .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 TUser instance with property: " + propertyName 
            + ", value: " + value); 
      try { 
         String queryString = "from TUser 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 findByUserName(Object userName) { 
		return findByProperty(USER_NAME, userName); 
	} 
	 
	public List findByPassword(Object password) { 
		return findByProperty(PASSWORD, password); 
	} 
	 
	public List findByNick(Object nick) { 
		return findByProperty(NICK, nick); 
	} 
	 
	public List findByEmail(Object email) { 
		return findByProperty(EMAIL, email); 
	} 
	 
	public List findByIsOpen(Object isOpen) { 
		return findByProperty(IS_OPEN, isOpen); 
	} 
	 
	public List findByCount(Object count) { 
		return findByProperty(COUNT, count); 
	} 
	 
	public List findByTeachers(Object teachers) { 
		return findByProperty(TEACHERS, teachers); 
	} 
	 
	public List findByName(Object name) { 
		return findByProperty(NAME, name); 
	} 
	 
	public List findBySex(Object sex) { 
		return findByProperty(SEX, sex); 
	} 
	 
	public List findByClass_(Object class_) { 
		return findByProperty(CLASS_, class_); 
	} 
	 
	public List findByTel(Object tel) { 
		return findByProperty(TEL, tel); 
	} 
	 
	public List findByIntro(Object intro) { 
		return findByProperty(INTRO, intro); 
	} 
	 
    public TUser merge(TUser detachedInstance) { 
        log.debug("merging TUser instance"); 
        try { 
            TUser result = (TUser) getSession() 
                    .merge(detachedInstance); 
            log.debug("merge successful"); 
            return result; 
        } catch (RuntimeException re) { 
            log.error("merge failed", re); 
            throw re; 
        } 
    } 
 
    public void attachDirty(TUser instance) { 
        log.debug("attaching dirty TUser instance"); 
        try { 
            getSession().saveOrUpdate(instance); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
     
    public void attachClean(TUser instance) { 
        log.debug("attaching clean TUser instance"); 
        try { 
            getSession().lock(instance, LockMode.NONE); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
}