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

📄 persondao.java

📁 QUICKSTARTS SPRING QUICKSTARTS SPRING QUICKSTARTS SPRING
💻 JAVA
字号:
package quickstart.db;

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.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Person.
 * 
 * @see quickstart.db.Person
 * @author MyEclipse Persistence Tools
 */

public class PersonDAO implements IPerson {
	private static final Log log = LogFactory.getLog(PersonDAO.class);
	private SessionFactory sessionFactory;
	
	// property constants
	public static final String LASTNAME = "lastname";
	public static final String FIRSTNAME = "firstname";

	public void save(Person transientInstance) {
		log.debug("saving Person instance");
		Session dbSession = sessionFactory.openSession();
		Transaction transaction = dbSession.beginTransaction();
		try {
			
			dbSession.save(transientInstance);
			transaction.commit();
			
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	public void remove(Integer id) {
		log.debug("deleting Person instance by id");
		Session dbSession = sessionFactory.openSession();
		try {
			Person persistentInstance = this.findById(id);
			dbSession.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	public void delete(Person persistentInstance) {
		log.debug("deleting Person instance");
		Session dbSession = sessionFactory.openSession();
		try {
			dbSession.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	public Person findById(java.lang.Integer id) {
		log.debug("getting Person instance with id: " + id);
		Session dbSession = sessionFactory.openSession();
		try {
			Person instance = (Person) dbSession.get("quickstart.db.Person", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Person> findByExample(Person instance) {
		log.debug("finding Person instance by example");
		Session dbSession = sessionFactory.openSession();
		try {
			List<Person> results = dbSession.createCriteria("quickstart.db.Person").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;
		}finally{
			dbSession.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Person> findByProperty(String propertyName, Object value) {
		log.debug("finding Person instance with property: " + propertyName
				+ ", value: " + value);
		Session dbSession = sessionFactory.openSession();
		try {
			String queryString = "from Person as model where model."
					+ propertyName + "= ?";
			Query queryObject = dbSession.createQuery(
					queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	public List<Person> findByLastname(Object lastname) {
		return findByProperty(LASTNAME, lastname);
	}

	public List<Person> findByFirstname(Object firstname) {
		return findByProperty(FIRSTNAME, firstname);
	}

	@SuppressWarnings("unchecked")
	public List<Person> findAll() {
		log.debug("finding all Person instances");
		Session dbSession = sessionFactory.openSession();
		try {
			String queryString = "from Person";
			Query queryObject = dbSession.createQuery(
					queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	public Person merge(Person detachedInstance) {
		log.debug("merging Person instance");
		Session dbSession = sessionFactory.openSession();
		try {
			Person result = (Person) dbSession.merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	public void attachDirty(Person instance) {
		log.debug("attaching dirty Person instance");
		Session dbSession = sessionFactory.openSession();
		try {
			dbSession.saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	public void attachClean(Person instance) {
		log.debug("attaching clean Person instance");
		Session dbSession = sessionFactory.openSession();
		try {
			dbSession.lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			dbSession.close();
		}
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}

⌨️ 快捷键说明

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