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


package edu.neu.sspp.hibernate; 
 
import java.util.Date; 
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 TProject. 
 * @see edu.neu.sspp.hibernate.TProject 
 * @author MyEclipse - Hibernate Tools 
 */ 
public class TProjectDAO extends BaseHibernateDAO { 
 
    private static final Log log = LogFactory.getLog(TProjectDAO.class); 
 
	//property constants 
	public static final String NAME = "name"; 
	public static final String INTRO = "intro"; 
	public static final String URL = "url"; 
	public static final String COUNT = "count"; 
	public static final String BROWSE = "browse"; 
 
     
    public void save(TProject transientInstance) { 
        log.debug("saving TProject instance"); 
        try { 
            getSession().save(transientInstance); 
            log.debug("save successful"); 
        } catch (RuntimeException re) { 
            log.error("save failed", re); 
            throw re; 
        } 
    } 
     
	public void delete(TProject persistentInstance) { 
        log.debug("deleting TProject instance"); 
        try { 
            getSession().delete(persistentInstance); 
            log.debug("delete successful"); 
        } catch (RuntimeException re) { 
            log.error("delete failed", re); 
            throw re; 
        } 
    } 
     
    public TProject findById( java.lang.String id) { 
        log.debug("getting TProject instance with id: " + id); 
        try { 
            TProject instance = (TProject) getSession() 
                    .get("edu.neu.sspp.hibernate.TProject", id); 
            return instance; 
        } catch (RuntimeException re) { 
            log.error("get failed", re); 
            throw re; 
        } 
    } 
     
    public int getCount() { 
    	log.debug("getting count of TProject"); 
    	 
    	try { 
        	String queryString = "select count(*) from TProject"; 
        	Query queryObject = getSession().createQuery(queryString); 
        	 
        	return ((Integer)queryObject.list().get(0)).intValue(); 
    	} catch (RuntimeException re) { 
    		log.error("getting count of TProject failed", re); 
    		throw re; 
    	} 
    } 
     
    public List getByPage(int first, int size) { 
    	log.debug("getting TProject for page"); 
    	 
    	try { 
    		//按日期降序排列 
        	String queryString = "from TProject as project order by project.dateTime 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 getCommProj() { 
    	log.debug("getting Commend TProject"); 
    	 
    	try { 
	    	String queryString = "from TProject as project where project.isCommend = 1 order by project.commendDate desc"; 
	    	Query queryObject = getSession().createQuery(queryString); 
	    	queryObject.setFirstResult(0); 
	    	queryObject.setMaxResults(edu.neu.sspp.Constant.index_count); 
    	return queryObject.list(); 
    	} catch(RuntimeException re) { 
    		log.error("get commend failed", re); 
    		throw re; 
    	} 
    	 
    	 
    } 
     
    public List findByExample(TProject instance) { 
        log.debug("finding TProject instance by example"); 
        try { 
            List results = getSession() 
                    .createCriteria("edu.neu.sspp.hibernate.TProject") 
                    .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 TProject instance with property: " + propertyName 
            + ", value: " + value); 
      try { 
         String queryString = "from TProject 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 findByIntro(Object intro) { 
		return findByProperty(INTRO, intro); 
	} 
	 
	public List findByUrl(Object url) { 
		return findByProperty(URL, url); 
	} 
	 
	public List findByCount(Object count) { 
		return findByProperty(COUNT, count); 
	} 
	 
	public List findByBrowse(Object browse) { 
		return findByProperty(BROWSE, browse); 
	} 
	 
    public TProject merge(TProject detachedInstance) { 
        log.debug("merging TProject instance"); 
        try { 
            TProject result = (TProject) getSession() 
                    .merge(detachedInstance); 
            log.debug("merge successful"); 
            return result; 
        } catch (RuntimeException re) { 
            log.error("merge failed", re); 
            throw re; 
        } 
    } 
 
    public void attachDirty(TProject instance) { 
        log.debug("attaching dirty TProject instance"); 
        try { 
            getSession().saveOrUpdate(instance); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
     
    public void attachClean(TProject instance) { 
        log.debug("attaching clean TProject instance"); 
        try { 
            getSession().lock(instance, LockMode.NONE); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
}