daosupporthibernateimp.java

来自「移动彩信管理平台」· Java 代码 · 共 196 行

JAVA
196
字号
/*
 * Created on 2006-11-22
 *
 */
package com.my7g.zj.mobile.mms.sys;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class DaoSupportHibernateImp extends HibernateDaoSupport implements
		DaoSupport {

	private static final Logger log = Logger
			.getLogger(DaoSupportHibernateImp.class);

	public Serializable save(Object arg0) {
		log.debug("insert " + getClassName(arg0) + " instance...");

		try {
			return this.getHibernateTemplate().save(arg0);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		}

	}

	public void saveOrUpdateAll(Collection arg0) {
		try {
			this.getHibernateTemplate().saveOrUpdateAll(arg0);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		}

	}

	public void saveOrUpdate(Object arg0) {
		try {
			this.getHibernateTemplate().saveOrUpdate(arg0);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		}

	}

	public void update(Object arg0) {

		try {
			this.getHibernateTemplate().update(arg0);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		}
	}

	public void delete(Object arg0) {

		try {
			this.getHibernateTemplate().delete(arg0);
		} catch (Exception e) {
			log.error(arg0);
			throw new RuntimeException();
		}
	}

	public void delete(Class c, Serializable s) {

		Object vo = this.get(c, s);
		this.delete(vo);
	}

	public void delete(Collection arg0) {

		try {
			this.getHibernateTemplate().deleteAll(arg0);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		}
	}

	public Object get(Class c, Serializable s) {

		try {
			return this.getHibernateTemplate().get(c, s);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		}
	}

	public List find(String hql) {

		try {
			return this.getHibernateTemplate().find(hql);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		}
	}

	public List find(String hql, int maxResult, int firstRow) {
		Session session = null;
		try {

			session = this.getSession();			
			Query query = session.createQuery(hql);
			query.setMaxResults(maxResult);
			query.setFirstResult(firstRow);

			return query.list();
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public List find(String arg0, Object[] arg1) {

		try {
			return this.getHibernateTemplate().find(arg0, arg1);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		}
	}

	public Long getResultCount(String hql) {
		Session session = null;
		try {
			session = this.getSession();
			Query query = session.createQuery(hql);
			return (Long) query.uniqueResult();

		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}
	
	public Object getUniqueResult(String hql) {
		Session session = null;
		try {
			session = this.getSession();
			Query query = session.createQuery(hql);
			return query.uniqueResult();

		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	private String getClassName(String className) {
		return className.substring(className.lastIndexOf('.') + 1);
	}

	private String getClassName(Object obj) {
		return obj == null ? "" : getClassName(obj.getClass().getName());
	}

	public Session getaSession() {

		return this.getSession();
	}

	public HibernateTemplate getaHibernateTemplate() {
		return this.getHibernateTemplate();
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?