public Entity create(Entity entity) throws DaoException {
try {
getHibernateTemplate().save(entity);
return entity;
} catch (DataAccessException e) {
throw new DaoException("保存 " + entity.getClass().getName()
+ " 实例到数据库失败", e);
}
}
public void update(Entity entity)throws DaoException {
try {
getHibernateTemplate().update(entity);
} catch (DataAccessException e) {
throw new DaoException("更新 " + entity.getClass().getName()
+ " 实例到数据库失败", e);
}
}
public void delete(Entity entity)throws DaoException {
try {
getHibernateTemplate().delete(entity);
} catch (DataAccessException e) {
throw new DaoException("从数据库删除 " + entity.getClass().getName()
+ " 实例失败", e);
}
}
public void deleteAll(Class clazz)throws DaoException {
try {
List result = getHibernateTemplate().loadAll(clazz);
getHibernateTemplate().deleteAll(result);
} catch (DataAccessException e) {
log.error("从数据库删除 " + clazz.getName() + "的所有记录失败", e);
throw new DaoException("从数据库删除 " + clazz.getName() + "的所有记录失败", e);
}
}
public void deleteAll(Collection entities)throws DaoException {
try {
getHibernateTemplate().deleteAll(entities);
} catch(DataAccessException e) {
throw new DaoException(e);
}
}
public Object loadByKey(Class clazz,String keyName, Object keyValue)
throws DaoException {
try {
List result = getHibernateTemplate().find(
"from " + clazz.getName() + " where " + keyName + " = ?",
keyValue);
if (result != null && result.size() > 0) {
return result.get(0);
} else {
return null;
}
} catch (DataAccessException e) {
throw new DaoException("加载 " + keyName + "为 " + keyValue + "的 "
+ clazz.getName() + " 实例失败", e);
}
}
public List loadAll(Class clazz)throws DaoException {
try {
return getHibernateTemplate().loadAll(clazz);
} catch (DataAccessException e) {
throw new DaoException("加载所有 " + clazz.getName() + "实例时失败", e);
}
}
public List find(String queryString) throws DaoException {
try {
return getHibernateTemplate().find(queryString);
} catch (DataAccessException e) {
throw new DaoException("执行查询 " + queryString + "失败", e);
}
}
public List find(String queryString, Object param) throws DaoException {
try {
return getHibernateTemplate().find(queryString, param);
} catch (DataAccessException e) {
throw new DaoException("执行参数为 " + param + "的查询 " + queryString
+ " 失败", e);
}
}
public List find(String queryString, Object[] params) throws DaoException {
try {
return getHibernateTemplate().find(queryString, params);
} catch (DataAccessException e) {
StringBuffer paramString = newStringBuffer("");
for (int i = 0; i < params.length; i++) {
paramString.append(params[i]);
paramString.append(" ");
}
throw new DaoException("执行参数为 " + paramString + "的查询"
+ queryString + " 失败", e);
}
}
public List findByNamedQuery(String queryName)throws DaoException {
try {
return getHibernateTemplate().findByNamedQuery(queryName);
} catch (DataAccessException e) {
throw new DaoException("执行命名为 " + queryName + "的查询失败");
}
}
public List findByNamedQuery(String queryName,Object param)
throws DaoException {
try {
return getHibernateTemplate().findByNamedQuery(queryName, param);
} catch (DataAccessException e) {
throw new DaoException("执行参数为 " + param + "命名为 " + queryName
+ " 的查询失败");
}
}
public List findByNameQuery(String queryName, Object[] params)
throws DaoException {
try {
return getHibernateTemplate().findByNamedQuery(queryName, params);
} catch (DataAccessException e) {
StringBuffer paramString = newStringBuffer("");
for (int i = 0; i < params.length; i++) {
paramString.append(params[i]);
paramString.append(" ");
}
throw new DaoException("执行参数为 " + paramString + "命名为" + queryName
+ " 的查询失败");
}
}
try {
getHibernateTemplate().save(entity);
return entity;
} catch (DataAccessException e) {
throw new DaoException("保存 " + entity.getClass().getName()
+ " 实例到数据库失败", e);
}
}
public void update(Entity entity)throws DaoException {
try {
getHibernateTemplate().update(entity);
} catch (DataAccessException e) {
throw new DaoException("更新 " + entity.getClass().getName()
+ " 实例到数据库失败", e);
}
}
public void delete(Entity entity)throws DaoException {
try {
getHibernateTemplate().delete(entity);
} catch (DataAccessException e) {
throw new DaoException("从数据库删除 " + entity.getClass().getName()
+ " 实例失败", e);
}
}
public void deleteAll(Class clazz)throws DaoException {
try {
List result = getHibernateTemplate().loadAll(clazz);
getHibernateTemplate().deleteAll(result);
} catch (DataAccessException e) {
log.error("从数据库删除 " + clazz.getName() + "的所有记录失败", e);
throw new DaoException("从数据库删除 " + clazz.getName() + "的所有记录失败", e);
}
}
public void deleteAll(Collection entities)throws DaoException {
try {
getHibernateTemplate().deleteAll(entities);
} catch(DataAccessException e) {
throw new DaoException(e);
}
}
public Object loadByKey(Class clazz,String keyName, Object keyValue)
throws DaoException {
try {
List result = getHibernateTemplate().find(
"from " + clazz.getName() + " where " + keyName + " = ?",
keyValue);
if (result != null && result.size() > 0) {
return result.get(0);
} else {
return null;
}
} catch (DataAccessException e) {
throw new DaoException("加载 " + keyName + "为 " + keyValue + "的 "
+ clazz.getName() + " 实例失败", e);
}
}
public List loadAll(Class clazz)throws DaoException {
try {
return getHibernateTemplate().loadAll(clazz);
} catch (DataAccessException e) {
throw new DaoException("加载所有 " + clazz.getName() + "实例时失败", e);
}
}
public List find(String queryString) throws DaoException {
try {
return getHibernateTemplate().find(queryString);
} catch (DataAccessException e) {
throw new DaoException("执行查询 " + queryString + "失败", e);
}
}
public List find(String queryString, Object param) throws DaoException {
try {
return getHibernateTemplate().find(queryString, param);
} catch (DataAccessException e) {
throw new DaoException("执行参数为 " + param + "的查询 " + queryString
+ " 失败", e);
}
}
public List find(String queryString, Object[] params) throws DaoException {
try {
return getHibernateTemplate().find(queryString, params);
} catch (DataAccessException e) {
StringBuffer paramString = newStringBuffer("");
for (int i = 0; i < params.length; i++) {
paramString.append(params[i]);
paramString.append(" ");
}
throw new DaoException("执行参数为 " + paramString + "的查询"
+ queryString + " 失败", e);
}
}
public List findByNamedQuery(String queryName)throws DaoException {
try {
return getHibernateTemplate().findByNamedQuery(queryName);
} catch (DataAccessException e) {
throw new DaoException("执行命名为 " + queryName + "的查询失败");
}
}
public List findByNamedQuery(String queryName,Object param)
throws DaoException {
try {
return getHibernateTemplate().findByNamedQuery(queryName, param);
} catch (DataAccessException e) {
throw new DaoException("执行参数为 " + param + "命名为 " + queryName
+ " 的查询失败");
}
}
public List findByNameQuery(String queryName, Object[] params)
throws DaoException {
try {
return getHibernateTemplate().findByNamedQuery(queryName, params);
} catch (DataAccessException e) {
StringBuffer paramString = newStringBuffer("");
for (int i = 0; i < params.length; i++) {
paramString.append(params[i]);
paramString.append(" ");
}
throw new DaoException("执行参数为 " + paramString + "命名为" + queryName
+ " 的查询失败");
}
}