Hibernate DAO 类
package org.huy.fram.hibernate;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;


/** *//**
*
* <p>
* Title:[Hibernate Data Access Object Support]
* </p>
* <p>
* Description: [操作数据库支持类,其它DAO可能继承该类]
* </p>
* <p>
* Copyright 2009 Huyvanpull Co., Ltd.
* </p>
* <p>
* All right reserved.
* </p>
*
* @version 1.0
*
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
*/
public class HibDAOSupport


{

/** *//** DAO的操作po的类型 */
private Class<?> claz = null;

/** *//** 构造方法,且只应提供此种构造方法 */
public HibDAOSupport(Class<?> claz)

{
this.claz = claz;
}

/** *//**
*
* <p>
* Description:[加载对象,返回代理实例]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param id
* @return
*/
public Object load(Serializable id)

{
Object entity = null;
try

{
Session session = HibernateSessionFactory.getSession();
entity = session.load(this.claz, id);
}
catch (RuntimeException re)

{
throw re;
}
return entity;
}

/** *//**
*
* <p>
* Description:[得到对象,返回实体类]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param id
* @return
*/
public Object get(Serializable id)

{
Object entity = null;
try

{
Session session = HibernateSessionFactory.getSession();
entity = session.get(this.claz, id);
}
catch (RuntimeException re)

{
throw re;
}
return entity;
}

/** *//**
*
* <p>
* Description:[把实例保存到数据库]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param entity
* @return
*/
public Object save(Object entity)

{
try

{

/** *//** 如果entity不是claz的实例,此处会抛出异常,下面代码不会执行 */
this.isInstance(entity);
Session session = HibernateSessionFactory.getSession();
session.save(entity);
}
catch (RuntimeException re)

{
throw re;
}
return entity;
}

/** *//**
*
* <p>
* Description:[把实例保存到数据库]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param entity
* @return
*/
public Object update(Object entity)

{
try

{

/** *//** 如果entity不是claz的实例,此处会抛出异常,下面代码不会执行 */
this.isInstance(entity);
Session session = HibernateSessionFactory.getSession();
session.update(entity);
}
catch (RuntimeException re)

{
throw re;
}
return entity;
}

/** *//**
*
* <p>
* Description:[根据实列删除对象]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param object
* @return
*/
public Object delete(Object entity)

{
try

{

/** *//** 如果entity不是claz的实例,此处会抛出异常,下面代码不会执行 */
this.isInstance(entity);
Session session = HibernateSessionFactory.getSession();
session.delete(entity);
}
catch (RuntimeException re)

{
throw re;
}
return entity;
}

/** *//**
*
* <p>
* Description:[根据id删除对象]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param id
*/
public void deleteById(Serializable id)

{
delete(load(id));
}

/** *//**
*
* <p>
* Description:[不附加条件查询所有对象]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @return
*/
public List<?> findAll()

{
String hsql = "from ".concat(this.getEntityClassName());
return this.find(hsql, 0, 0);
}

/** *//**
*
* <p>
* Description:[根据hsql查询数据]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 30, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param hsql
* HSQL
* @param beginIndex
* 查询起始位置
* @param pageSize
* 每页长度
* @return
*/
public List<?> find(final String hsql, final int beginIndex,
final int pageSize)

{
List<?> entityLst = null;
try

{
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hsql);
if (beginIndex != 0)

{
query.setFirstResult(beginIndex);
}