HibernateTemplate提供的方法使用

HibernateTemplate提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作, Spring 2.0更增加对命名 SQL查询的支持,也增加对分页的支持。大部分情况下,使用 Hibernate的常规用法,就可完成大多数 DAO对象的 CRUD操作。下面是 HibernateTemplate的常用方法简介:

q      void delete(Object entity):删除指定持久化实例

q      deleteAll(Collection entities):删除集合内全部持久化类实例

q      find(String queryString):根据 HQL查询字符串来返回实例集合

q      findByNamedQuery(String queryName):根据命名查询返回实例集合

q      get(Class entityClass, Serializable id):根据主键加载特定持久化类的实例

q      save(Object entity):保存新的实例

q      saveOrUpdate(Object entity):根据实例状态,选择保存或者更新

q      update(Object entity):更新实例的状态,要求 entity是持久状态

q      setMaxResults(int maxResults):设置分页的大小

 

package com.yp.hibernate.model;

// default package

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yp.dtos.TicketDTO;

/**
 * Data access object (DAO) for domain model class BpMovie.
 *
 * @see .BpMovie
 * @author MyEclipse - Hibernate Tools
 */
public class BpMovieDAO extends HibernateDaoSupport {

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

 private HibernateTemplate hibernateTemplate;

 private SessionFactory sessionFactory;

 public BpMovieDAO() {
 }

 public BpMovieDAO(SessionFactory sessionFactory) {
  // TODO Auto-generated constructor stub
  this.sessionFactory = sessionFactory;
  this.hibernateTemplate = new HibernateTemplate(this.sessionFactory);
 }

 protected void initDao() {
  // do nothing
 }

 public void save(BpMovie transientInstance) {
  log.debug("saving BpMovie instance");
  try {
   getHibernateTemplate().saveOrUpdate(transientInstance);
   log.debug("save successful");
  } catch (RuntimeException re) {
   log.error("save failed", re);
   throw re;
  }
 }

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

 public BpMovie findById(java.lang.Integer id) {
  log.debug("getting BpMovie instance with id: " + id);
  try {
   BpMovie instance = (BpMovie) this.hibernateTemplate.get("com.yp.hibernate.model.BpMovie",id);
   return instance;
  } catch (RuntimeException re) {
   log.error("get failed", re);
   throw re;
  }
 }

 public List findByExample(BpMovie instance) {
  log.debug("finding BpMovie instance by example");
  try {
   List results = getSession().createCriteria("BpMovie").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 void attachDirty(BpMovie instance) {
  log.debug("attaching dirty BpMovie instance");
  try {
   getHibernateTemplate().saveOrUpdate(instance);
   log.debug("attach successful");
  } catch (RuntimeException re) {
   log.error("attach failed", re);
   throw re;
  }
 }

//将传入的对象状态设置为Transient态

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

 public static BpMovieDAO getFromApplicationContext(ApplicationContext ctx) {
  return (BpMovieDAO) ctx.getBean("BpMovieDAO");
 }

 }


你可能感兴趣的:(DAO,Hibernate,object,MyEclipse,delete,Constructor)