www.pudn.com > SSPP.rar > TMessageDAO.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 TMessage. 
 * @see edu.neu.sspp.hibernate.TMessage 
 * @author MyEclipse - Hibernate Tools 
 */ 
public class TMessageDAO extends BaseHibernateDAO { 
 
    private static final Log log = LogFactory.getLog(TMessageDAO.class); 
 
	//property constants 
	public static final String MESSAGE = "message"; 
 
     
    public void save(TMessage transientInstance) { 
        log.debug("saving TMessage instance"); 
        try { 
            getSession().save(transientInstance); 
            log.debug("save successful"); 
        } catch (RuntimeException re) { 
            log.error("save failed", re); 
            throw re; 
        } 
    } 
     
	public void delete(TMessage persistentInstance) { 
        log.debug("deleting TMessage instance"); 
        try { 
            getSession().delete(persistentInstance); 
            log.debug("delete successful"); 
        } catch (RuntimeException re) { 
            log.error("delete failed", re); 
            throw re; 
        } 
    } 
	 
	public TMessage getMessage() { 
		log.debug("getting message!"); 
    	try { 
	    	String queryString = "from TMessage as message order by message.dateTime desc"; 
	    	Query queryObject = getSession().createQuery(queryString); 
	    	queryObject.setFirstResult(0); 
	    	queryObject.setMaxResults(1); 
	    	 
	    	List list = queryObject.list(); 
	    	if(list.isEmpty()) 
	    		return new TMessage("", new Date()); 
	    	else 
	    		return (TMessage)queryObject.list().get(0); 
    	} catch(RuntimeException re) { 
    		log.error("get commend failed", re); 
    		throw re; 
    	} 
	} 
     
    public TMessage findById( java.lang.String id) { 
        log.debug("getting TMessage instance with id: " + id); 
        try { 
            TMessage instance = (TMessage) getSession() 
                    .get("edu.neu.sspp.hibernate.TMessage", id); 
            return instance; 
        } catch (RuntimeException re) { 
            log.error("get failed", re); 
            throw re; 
        } 
    } 
     
     
    public List findByExample(TMessage instance) { 
        log.debug("finding TMessage instance by example"); 
        try { 
            List results = getSession() 
                    .createCriteria("edu.neu.sspp.hibernate.TMessage") 
                    .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 TMessage instance with property: " + propertyName 
            + ", value: " + value); 
      try { 
         String queryString = "from TMessage 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 findByMessage(Object message) { 
		return findByProperty(MESSAGE, message); 
	} 
	 
    public TMessage merge(TMessage detachedInstance) { 
        log.debug("merging TMessage instance"); 
        try { 
            TMessage result = (TMessage) getSession() 
                    .merge(detachedInstance); 
            log.debug("merge successful"); 
            return result; 
        } catch (RuntimeException re) { 
            log.error("merge failed", re); 
            throw re; 
        } 
    } 
 
    public void attachDirty(TMessage instance) { 
        log.debug("attaching dirty TMessage instance"); 
        try { 
            getSession().saveOrUpdate(instance); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
     
    public void attachClean(TMessage instance) { 
        log.debug("attaching clean TMessage instance"); 
        try { 
            getSession().lock(instance, LockMode.NONE); 
            log.debug("attach successful"); 
        } catch (RuntimeException re) { 
            log.error("attach failed", re); 
            throw re; 
        } 
    } 
}