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);
}
if (pageSize != 0)
{
query.setMaxResults(pageSize);
}
entityLst = query.list();
}
catch (RuntimeException re)
{
throw re;
}
return entityLst;
}
/** *//**
*
* <p>
* Description:[根据SQL查询数据]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 30, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param sql
* SQL
* @param beginIndex
* 查询起始位置
* @param pageSize
* 每页长度
* @return
*/
public List<?> findBySql(final String sql, final int beginIndex,
final int pageSize)
{
List<?> entityLst = null;
try
{
Session session = HibernateSessionFactory.getSession();
Query query = session.createSQLQuery(sql);
if (beginIndex != 0)
{
query.setFirstResult(beginIndex);
}
if (pageSize != 0)
{
query.setMaxResults(pageSize);
}
entityLst = query.list();
}
catch (RuntimeException re)
{
throw re;
}
return entityLst;
}
/** *//**
*
* <p>
* Description:[根据HQL统计合符条件的数目]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param hql
* @return
*/
public int count(final String hql)
{
int count = 0;
try
{
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
count = ((Integer) query.uniqueResult()).intValue();
}
catch (RuntimeException re)
{
throw re;
}
return count;
}
/** *//**
*
* <p>
* Description:[根据原生SQL统计合符条件的数目]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param sql
* @return
*/
public int countBySql(final String sql)
{
int count = 0;
try
{
Session session = HibernateSessionFactory.getSession();
Query query = session.createSQLQuery(sql);
count = ((BigDecimal) query.uniqueResult()).intValue();
}
catch (RuntimeException re)
{
throw re;
}
return count;
}
/** *//**
*
* <p>
* Description:[执行原生SQL,返回更改的条数]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param sql
* @return
*/
public int executeSQL(final String sql)
{
int updateCount = 0;
try
{
Session session = HibernateSessionFactory.getSession();
Query query = session.createSQLQuery(sql);
updateCount = query.executeUpdate();
}
catch (RuntimeException re)
{
throw re;
}
return updateCount;
}
/** *//**
*
* <p>
* Description:[如果不是claz的实例,则抛出异常]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param entity
*/
private void isInstance(Object entity)
{
if (!this.claz.isInstance(entity))
{
/** *//** 如果所传的对象不是claz的实例,则抛出异常由上一级进行处理 */
StringBuffer exceptionInfo = new StringBuffer(512);
exceptionInfo.append("entity is not instance of ");
exceptionInfo.append(this.claz.getName());
exceptionInfo.trimToSize();
throw new RuntimeException(exceptionInfo.toString());
}
}
/** *//**
*
* <p>
* Description:[本方法用于得到claz的名字]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @return
*/
private String getEntityClassName()
{
String name = this.claz.getName();
/** *//** 得到类名 */
return name.substring(name.lastIndexOf(".") + 1, name.length());
}
}
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);
}
if (pageSize != 0)
{
query.setMaxResults(pageSize);
}
entityLst = query.list();
}
catch (RuntimeException re)
{
throw re;
}
return entityLst;
}
/** *//**
*
* <p>
* Description:[根据SQL查询数据]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 30, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param sql
* SQL
* @param beginIndex
* 查询起始位置
* @param pageSize
* 每页长度
* @return
*/
public List<?> findBySql(final String sql, final int beginIndex,
final int pageSize)
{
List<?> entityLst = null;
try
{
Session session = HibernateSessionFactory.getSession();
Query query = session.createSQLQuery(sql);
if (beginIndex != 0)
{
query.setFirstResult(beginIndex);
}
if (pageSize != 0)
{
query.setMaxResults(pageSize);
}
entityLst = query.list();
}
catch (RuntimeException re)
{
throw re;
}
return entityLst;
}
/** *//**
*
* <p>
* Description:[根据HQL统计合符条件的数目]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param hql
* @return
*/
public int count(final String hql)
{
int count = 0;
try
{
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
count = ((Integer) query.uniqueResult()).intValue();
}
catch (RuntimeException re)
{
throw re;
}
return count;
}
/** *//**
*
* <p>
* Description:[根据原生SQL统计合符条件的数目]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param sql
* @return
*/
public int countBySql(final String sql)
{
int count = 0;
try
{
Session session = HibernateSessionFactory.getSession();
Query query = session.createSQLQuery(sql);
count = ((BigDecimal) query.uniqueResult()).intValue();
}
catch (RuntimeException re)
{
throw re;
}
return count;
}
/** *//**
*
* <p>
* Description:[执行原生SQL,返回更改的条数]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param sql
* @return
*/
public int executeSQL(final String sql)
{
int updateCount = 0;
try
{
Session session = HibernateSessionFactory.getSession();
Query query = session.createSQLQuery(sql);
updateCount = query.executeUpdate();
}
catch (RuntimeException re)
{
throw re;
}
return updateCount;
}
/** *//**
*
* <p>
* Description:[如果不是claz的实例,则抛出异常]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @param entity
*/
private void isInstance(Object entity)
{
if (!this.claz.isInstance(entity))
{
/** *//** 如果所传的对象不是claz的实例,则抛出异常由上一级进行处理 */
StringBuffer exceptionInfo = new StringBuffer(512);
exceptionInfo.append("entity is not instance of ");
exceptionInfo.append(this.claz.getName());
exceptionInfo.trimToSize();
throw new RuntimeException(exceptionInfo.toString());
}
}
/** *//**
*
* <p>
* Description:[本方法用于得到claz的名字]
* </p>
* <p>
* Created by [Huyvanpull] [Jun 26, 2009]
* </p>
* <p>
* Midified by [修改人] [修改时间]
* </p>
*
* @return
*/
private String getEntityClassName()
{
String name = this.claz.getName();
/** *//** 得到类名 */
return name.substring(name.lastIndexOf(".") + 1, name.length());
}
}
DAO继承该类,几乎不用写代码了.