⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 department1dao.java

📁 hibernate应用测试,初学hibernate 的会员可以看看.
💻 JAVA
字号:
package gdpe.hibernate.department;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
//import org.hibernate.Session;
//import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.HibernateException;
import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;

/**
 * Data access object (DAO) for domain model class Department1.
 * 
 * @see gdpe.hibernate.department.Department1
 * @author MyEclipse - Hibernate Tools
 */
public class Department1DAO extends BaseHibernateDAO {

	private static final Log log = LogFactory.getLog(Department1DAO.class);

	// property constants
	public static final String CHDEPTNAME = "chdeptname";

	public static final String CHDEPTDESC = "chdeptdesc";

	public static final String CHPARENTDEPT = "chparentdept";

	public static final String CHDEPTMANAGER = "chdeptmanager";

	public static final String CHOPENDATE = "chopendate";

	public static final String CHCLOSEFLAG = "chcloseflag";

	public static final String CHPHONE = "chphone";

	public static final String CHCLOSEDATE = "chclosedate";

	public static final String CHFAX = "chfax";

	public static final String CHADDRESS = "chaddress";

	public static final String CHZIPCODE = "chzipcode";

	public static final String CHEMAIL = "chemail";

	public static final String CHMODIFIER = "chmodifier";

	// public static static Session session1222=null;

	// public static void startTransaction() {
	// this.getSession.betinTransaction();
	// }
	//	
	public void save(Department1 transientInstance) {
		log.debug("saving Department1 instance");
		Transaction tx = null;
		Session session = getSession();
		try {
			tx = session.beginTransaction();
			session.save(transientInstance);
			tx.commit();
			// endTransaction(true);
			System.out.println("------------>增加成功");
			log.debug("增加成功");
		} catch (RuntimeException re) {
			tx.rollback();
			log.error("save failed", re);
			throw re;

		} finally {
			close(session);
		}
	}

	public void delete(Department1 persistentInstance) {
		log.debug("deleting Department1 instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void update(Department1 dep) {
		Transaction tx = null;
		Session session = getSession();
		try {
			tx = session.beginTransaction();
			session.update(dep);
			tx.commit();
			// endTransaction(true);
			System.out.println("------------>修改成功");
			log.debug("修改成功");
		} catch (RuntimeException re) {
			tx.rollback();
			log.error("save failed", re);
			throw re;
		} finally {
			close(session);
		}
	}
	public Department1 findById(java.lang.String id) {
		log.debug("getting Department1 instance with id: " + id);
		Transaction tx = null;
		Session session = getSession();
		try {
			
			tx = session.beginTransaction();
			Department1 instance = (Department1) session.get(
					"gdpe.hibernate.department.Department1", id);
			tx.commit();
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			tx.rollback();
			throw re;
		}finally {
			close(session);
		}
	}

	public List findByExample(Department1 instance) {
		log.debug("finding Department1 instance by example");
		try {
			List results = getSession().createCriteria(
					"gdpe.hibernate.department.Department1").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 Department1 instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Department1 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 findByChdeptname(Object chdeptname) {
		return findByProperty(CHDEPTNAME, chdeptname);
	}

	public List findByChdeptdesc(Object chdeptdesc) {
		return findByProperty(CHDEPTDESC, chdeptdesc);
	}

	public List findByChparentdept(Object chparentdept) {
		return findByProperty(CHPARENTDEPT, chparentdept);
	}

	public List findByChdeptmanager(Object chdeptmanager) {
		return findByProperty(CHDEPTMANAGER, chdeptmanager);
	}

	public List findByChopendate(Object chopendate) {
		return findByProperty(CHOPENDATE, chopendate);
	}

	public List findByChcloseflag(Object chcloseflag) {
		return findByProperty(CHCLOSEFLAG, chcloseflag);
	}

	public List findByChphone(Object chphone) {
		return findByProperty(CHPHONE, chphone);
	}

	public List findByChclosedate(Object chclosedate) {
		return findByProperty(CHCLOSEDATE, chclosedate);
	}

	public List findByChfax(Object chfax) {
		return findByProperty(CHFAX, chfax);
	}

	public List findByChaddress(Object chaddress) {
		return findByProperty(CHADDRESS, chaddress);
	}

	public List findByChzipcode(Object chzipcode) {
		return findByProperty(CHZIPCODE, chzipcode);
	}

	public List findByChemail(Object chemail) {
		return findByProperty(CHEMAIL, chemail);
	}

	public List findByChmodifier(Object chmodifier) {
		return findByProperty(CHMODIFIER, chmodifier);
	}

	public Department1 merge(Department1 detachedInstance) {
		log.debug("merging Department1 instance");
		try {
			Department1 result = (Department1) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Department1 instance) {
		log.debug("attaching dirty Department1 instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Department1 instance) {
		log.debug("attaching clean Department1 instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public Collection findWithPage(int pageSize, int startRow)
			throws HibernateException {
		Collection vehicleList = null;
		Transaction tx = null;
		Session session = getSession();
		try { // 这里需要得到传来的参数
			tx = session.beginTransaction();
			Query q = session.createQuery("from Department1 dp");
			q.setFirstResult(startRow);
			q.setMaxResults(pageSize);
			vehicleList = q.list();
			tx.commit();
			// endTransaction(true);
		} catch (HibernateException e) {
			tx.rollback();
		} finally {
			close(session);
		}
		return vehicleList;
	}

	public Collection findWithPage(String questr, int pageSize, int startRow)
			throws HibernateException {
		Collection vehicleList = null;
		Transaction tx = null;
		Session session = getSession();
		try { // 这里需要得到传来的参数
			tx = session.beginTransaction();
			Query q = session.createQuery(questr);
			q.setFirstResult(startRow);
			q.setMaxResults(pageSize);
			vehicleList = q.list();
			tx.commit();
			// endTransaction(true);
		} catch (HibernateException e) {
			tx.rollback();
		} finally {
			close(session);
		}
		return vehicleList;
	}

	public Collection findWithPage(HttpServletRequest req, int pageSize,
			int startRow) throws HibernateException {
		Collection vehicleList = null;
		Transaction tx = null;
		Session session = getSession();
		try { // 这里需要得到传来的参数
			tx = session.beginTransaction();

			String que = null;

			String re = req.getParameter("flag");
			if (re.equals("one")) {
				que = "from Department1 as dp";
				try {
					Query q = session.createQuery(que);
					q.setFirstResult(startRow);
					q.setMaxResults(pageSize);
					vehicleList = q.list();
					tx.commit();
					// endTransaction(true);
				} catch (HibernateException he) {
					tx.rollback();
					// System.out.println("chaxuntiao---");
				} finally {
					close(session);
				}
				return vehicleList;

			}

			que = "from Department1 as dp ";
			String chdeptname = ((String) req.getParameter("chdeptname"));
			String chdeptmanager = ((String) req.getParameter("chdeptmanager"));
			String chdeptname1 = (String) req.getAttribute("chdeptname");
			int i = 0;
			// int f=0;
			// boolean t=false;
			Department1 depart = new Department1();

			if (chdeptname1 != null && !(chdeptname1 + "a").equals("a")
					&& !(chdeptname1 + "a").equals("nulla")) {
				if (i == 0) {
					que = que + " where " + " dp.chdeptname =:chdeptname";
					i++;
					depart.setChdeptname(chdeptname1);
					req.setAttribute("chdeptname", chdeptname1);
				}
			}

			if (chdeptname != null && !(chdeptname + "a").equals("a")) {
				if (i == 0) {
					que = que + " where " + "dp.chdeptname =:chdeptname";
					i++;
					depart.setChdeptname(chdeptname);
				}
			}
			if (chdeptmanager != null && !(chdeptmanager + "a").equals("a")) {
				if (i == 0) {
					que = que + " where "
							+ " dp.chdeptmanager =:chdeptmanager ";
					i++;
					depart.setChdeptmanager(chdeptmanager);
				} else {
					que = que + " and " + " dp.chdeptmanager =:chdeptmanager ";
					i++;
					depart.setChdeptmanager(chdeptmanager);
				}
			}
			Query q = session.createQuery(que);
			// q.setString("chdeptname", chdeptname);
			q.setProperties(depart);
			q.setFirstResult(startRow);
			q.setMaxResults(pageSize);
			vehicleList = q.list();
			tx.commit();
			// endTransaction(true);
		} catch (HibernateException e) {
			tx.rollback();
		} finally {
			close(session);
		}
		return vehicleList;
	}

	public int getRows(String que) throws HibernateException {
		int totalRows = 0;
		Transaction tx = null;
		Session session = getSession();
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery(que);// 这条语句慢
			totalRows = query.list().size();
			// totalRows =
			// ((Integer)getSession().createQuery(query).next()).intValue();
			tx.commit();
			// endTransaction(true);
		} catch (HibernateException he) {
			tx.rollback();
			// System.out.println("chaxuntiao---");
		} finally {
			close(session);
		}
		return totalRows;
	}

	public int getRows(HttpServletRequest req) {
		int totalRows = 0;
		// String que= getStr(req);
		String que = null;

		Transaction tx = null;
		Session session = getSession();
		try {
			tx = session.beginTransaction();// 这条语句慢

			String re = req.getParameter("flag");
			if (re.equals("one")) {
				que = "from Department1 as dp";
				String chdeptname = (String) req.getAttribute("chdeptname1");
				int i = 0;
				Department1 depart = new Department1();
				if (chdeptname != null && !(chdeptname + "a").equals("a")) {
					if (i == 0) {
						que = que + " where " + " dp.chdeptname =:chdeptname";
						i++;
						depart.setChdeptname(chdeptname);
						req.setAttribute("chdeptname", chdeptname);
					}
				}
				try {
					Query query = session.createQuery(que);
					query.setProperties(depart);
					totalRows = query.list().size();
					// totalRows =
					// ((Integer)getSession().createQuery(query).next()).intValue();
					tx.commit();
				} catch (HibernateException he) {
					tx.rollback();
					// System.out.println("chaxuntiao---");
				} finally {
					close(session);
				}
				return totalRows;

			}
			que = "from Department1 as dp ";
			String chdeptname = ((String) req.getParameter("chdeptname"));
			String chdeptmanager = ((String) req.getParameter("chdeptmanager"));
			String chdeptname1 = ((String) req.getParameter("chdeptname1"));
			int i = 0;
			Department1 depart = new Department1();
			if (chdeptname1 != null && !(chdeptname1 + "a").equals("a")
					&& !(chdeptname1 + "a").equals("nulla")) {
				if (i == 0) {
					que = que + " where " + " dp.chdeptname =:chdeptname";
					i++;
					depart.setChdeptname(chdeptname1);
					req.setAttribute("chdeptname", chdeptname1);
				}
			}
			if (chdeptname != null && !(chdeptname + "a").equals("a")) {
				if (i == 0) {
					que = que + " where " + " dp.chdeptname =:chdeptname";
					i++;
					depart.setChdeptname(chdeptname);
					req.setAttribute("chdeptname", chdeptname);
				}
			}
			if (chdeptmanager != null && !(chdeptmanager + "a").equals("a")) {
				if (i == 0) {
					que = que + " where "
							+ " dp.chdeptmanager =:chdeptmanager ";
					i++;
					// depart.setChdeptmanager(chdeptmanager);
				} else {
					que = que + " and " + " dp.chdeptmanager =:chdeptmanager ";
					i++;
					depart.setChdeptmanager(chdeptmanager);
				}
			}
			Query query = session.createQuery(que);
			query.setProperties(depart);
			totalRows = query.list().size();
			// totalRows =
			// ((Integer)getSession().createQuery(query).next()).intValue();
			tx.commit();
		} catch (HibernateException he) {
			tx.rollback();
		} finally {
			close(session);
		}

		return totalRows;
	}

	public String getStr(HttpServletRequest req) {
		String re = req.getParameter("flag");
		if (re.equals("one")) {
			String que = "from Department1 as dp";
			return que;
		} else {
			String que = "from Department1 as dp";
			String chdeptname = ((String) req.getParameter("chdeptname"))
					.trim();
			String chdeptmanager = ((String) req.getParameter("chdeptmanager"))
					.trim();
			int i = 0;

			if (chdeptname != null && !(chdeptname + "a").equals("a")) {
				if (i == 0) {
					que = que + "where " + "dp.chdepname =:chdeptname";
					i++;
				}
			}
			if (chdeptmanager != null && !(chdeptmanager + "a").equals("a")) {
				if (i == 0) {
					que = que + "where " + "dp.chdeptmanager like "
							+ chdeptmanager;
					i++;
				} else {
					que = que + " and " + "dp.chdeptmanager like "
							+ chdeptmanager;
					i++;
				}
			}
			return que;
		}

	}
}

⌨️ 快捷键说明

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