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


package edu.neu.sspp.hibernate; 
 
import java.util.Date; 
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 TComment. 
 * @see edu.neu.sspp.hibernate.TComment 
 * @author MyEclipse - Hibernate Tools 
 */ 
public class TCommentDAO extends BaseHibernateDAO { 
 
    private static final Log log = LogFactory.getLog(TCommentDAO.class); 
 
	//property constants 
	public static final String NICK = "nick"; 
	public static final String IS_USER = "isUser"; 
	public static final String CONTENT = "content"; 
	public static final String USER_NAME = "userName"; 
	public static final String TEACHER_EMAIL = "teacherEmail"; 
     
    public void save(TComment transientInstance) { 
        log.debug("saving TComment instance"); 
        try { 
            getSession().save(transientInstance); 
            log.debug("save successful"); 
        } catch (RuntimeException re) { 
            log.error("save failed", re); 
            throw re; 
        } 
    } 
     
	public void delete(TComment persistentInstance) { 
        log.debug("deleting TComment instance"); 
        try { 
            getSession().delete(persistentInstance); 
            log.debug("delete successful"); 
        } catch (RuntimeException re) { 
            log.error("delete failed", re); 
            throw re; 
        } 
    } 
     
    public TComment findById( java.lang.String id) { 
        log.debug("getting TComment instance with id: " + id); 
        try { 
            TComment instance = (TComment) getSession() 
                    .get("edu.neu.sspp.hibernate.TComment", id); 
            return instance; 
        } catch (RuntimeException re) { 
            log.error("get failed", re); 
            throw re; 
        } 
    } 
     
    public int getCount() { 
    	log.debug("getting count of TComment"); 
    	 
    	try { 
        	String queryString = "select count(*) from TComment"; 
        	Query queryObject = getSession().createQuery(queryString); 
        	 
        	return ((Integer)queryObject.list().get(0)).intValue(); 
    	} catch (RuntimeException re) { 
    		log.error("getting count of TComment failed", re); 
    		throw re; 
    	} 
    } 
     
    public List getByPage(int first, int size) { 
    	log.debug("getting TComment for page"); 
    	 
    	try { 
    		//按日期降序排列 
        	String queryString = "from TComment as comment order by comment.date desc"; 
        	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(TComment instance) { 
        log.debug("finding TComment instance by example"); 
        try { 
            List results = getSession() 
                    .createCriteria("edu.neu.sspp.hibernate.TComment") 
                    .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 TComment instance with property: " + propertyName 
            + ", value: " + value); 
      try { 
         String queryString = "from TComment 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 findByNick(Object nick) { 
		return findByProperty(NICK, nick); 
	} 
	 
	public List findByIsUser(Object isUser) { 
		return findByProperty(IS_USER, isUser); 
	} 
	 
	public List findByContent(Object content) { 
		return findByProperty(CONTENT, content); 
	} 
	 
	public List findByUserName(Object userName) { 
		return findByProperty(USER_NAME, userName); 
	} 
	 
	public List findByTeacherEmail(Object teacherEmail) { 
		return findByProperty(TEACHER_EMAIL, teacherEmail); 
	} 
	 
    public TComment merge(TComment detachedInstance) { 
        log.debug("merging TComment instance"); 
        try { 
            TComment result = (TComment) getSession() 
                    .merge(detachedInstance); 
            log.debug("merge successful"); 
            return result; 
        } catch (RuntimeException re) { 
            log.error("merge failed", re); 
            throw re; 
        } 
    } 
 
    public void attachDirty(TComment instance) { 
        log.debug("attaching dirty TComment instance"); 
        try { 
            getSession().saveOrUpdate(instance); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
     
    public void attachClean(TComment instance) { 
        log.debug("attaching clean TComment instance"); 
        try { 
            getSession().lock(instance, LockMode.NONE); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
}