注册
登录
搜索
标签
帮助
无线搜索论坛
无线搜索技术
开源项目 bba96中的DAO实现-3-HibernateDAO
上一主题
下一主题
发新话题
发布投票
发布商品
发布悬赏
发布活动
发布辩论
发布视频
打印
开源项目 bba96中的DAO实现-3-HibernateDAO
jeffer
jeffer
总司令


个人空间
发短消息
加为好友
当前离线
1#大 中小 发表于 2008-2-27 11:26 只看该作者
开源项目 bba96中的DAO实现-3-HibernateDAO
package com.bba96.core.dao.hibernate;
import static com.bba96.core.util.CoreConstants.DEFAULT_BATCH_SIZE;
import java.util.Collection;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.bba96.core.query.QueryObject;
import com.bba96.core.query.QueryProjections;
import com.bba96.core.util.CollectionUtils;
import com.bba96.core.util.IntegerUtils;
import com.bba96.core.util.Page;
/*** @author <a href="mailto:scorpio_leon@hotmail.com">Leon Li</a>
**/public class HibernateDAO extends BaseHibernateDAO {
/*** Persist some of given transient instances.
** @param some of transient instance of a persistent class
*/@SuppressWarnings("unchecked")
public void batchSave(final Collection transientInstances) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
HibernateDAOHelper.checkWriteOperationAllowed(getHibernateTemplate(), session);
if (CollectionUtils.isNotEmpty(transientInstances)) {
int max = CollectionUtils.size(transientInstances);
int i = 0;
for (Object t : transientInstances) {
session.save(t);
if ((i != 0 && i % DEFAULT_BATCH_SIZE == 0) || i == max - 1) {
session.flush();
session.clear();
}i++;}}return null;
}});}/*** Update some of persistent instances with the identifier of each of the given detached
* instances.
** @param some detached instances containing updated state
*/@SuppressWarnings("unchecked")
public void batchUpdate(final Collection detachedInstances) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
HibernateDAOHelper.checkWriteOperationAllowed(getHibernateTemplate(), session);
if (CollectionUtils.isNotEmpty(detachedInstances)) {
int max = CollectionUtils.size(detachedInstances);
int i = 0;
for (Object t : detachedInstances) {
session.update(t);
if ((i != 0 && i % DEFAULT_BATCH_SIZE == 0) || i == max - 1) {
session.flush();
session.clear();
}i++;}}return null;
}});}/*** Copy the state of the given object onto the persistent object with the same identifier.
** @param a detached instance with state to be copied
*/@SuppressWarnings("unchecked")
public void batchMerge(final Collection detachedInstances) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
HibernateDAOHelper.checkWriteOperationAllowed(getHibernateTemplate(), session);
if (CollectionUtils.isNotEmpty(detachedInstances)) {
int max = CollectionUtils.size(detachedInstances);
int i = 0;
for (Object t : detachedInstances) {
session.merge(t);
if ((i != 0 && i % DEFAULT_BATCH_SIZE == 0) || i == max - 1) {
session.flush();
session.clear();
}i++;}}return null;
}});}/*** Either <tt>save()</tt> or <tt>update()</tt> some of given instances。
** @param some detached instances with state to be copied
*/@SuppressWarnings("unchecked")
public void batchSaveOrUpdate(final Collection instances) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
HibernateDAOHelper.checkWriteOperationAllowed(getHibernateTemplate(), session);
if (CollectionUtils.isNotEmpty(instances)) {
int max = CollectionUtils.size(instances);
int i = 0;
for (Object t : instances) {
session.saveOrUpdate(t);
if ((i != 0 && i % DEFAULT_BATCH_SIZE == 0) || i == max - 1) {
session.flush();
session.clear();
}i++;}}return null;
}});}/*** Remove some persistent instances from the datastore.
** @param some instances to be removed
*/@SuppressWarnings("unchecked")
public void batchRemove(final Collection persistentInstances) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
HibernateDAOHelper.checkWriteOperationAllowed(getHibernateTemplate(), session);
if (CollectionUtils.isNotEmpty(persistentInstances)) {
int max = CollectionUtils.size(persistentInstances);
int i = 0;
for (Object t : persistentInstances) {
session.refresh(t);
session.delete(t);
if ((i != 0 && i % DEFAULT_BATCH_SIZE == 0) || i == max - 1) {
session.flush();
session.clear();
}i++;}}return null;
}});}/*** Return an instance of Page which contains some persistent instances by specific conditions.
** @param an instance contains conditions
* @param whether permit the use of caching
* @param starting index
* @param largest number of records
* @param whether permit pagination
* @return an instance of Page
* @see com.bba96.core.util.Page
*/@SuppressWarnings("unchecked")
public <T> Page<T> findByQueryObject(QueryObject<T> queryObject, boolean cacheable, int startIndex, int maxResultCount, boolean paginationSupport) {
// TODO aspectJ
HibernateDAOHelper.validateQueryObject(queryObject);
int totalCount = -1;
List<T> result = null;
if (paginationSupport) {
/** Get the number of result which carried out by hibernate's criteria query first when paginationSupport's value is true.
*/QueryProjections backupProjections = null;
if (queryObject.getQueryProjections() == null) {
queryObject.setQueryProjections(new QueryProjections());
backupProjections = queryObject.getQueryProjections();
}if (backupProjections == null) {
backupProjections = queryObject.getQueryProjections().clone();
}QueryProjections rowCountProjections = new QueryProjections();
rowCountProjections.setRowCount(true);
rowCountProjections.setDistinct(backupProjections.getDistinct());
rowCountProjections.setDistinctFlag(backupProjections.isDistinctFlag());
rowCountProjections.setGroupProperty(backupProjections.getGroupProperty());
queryObject.setQueryProjections(rowCountProjections);
List countResult = findListByQueryObject(queryObject, true, -1, -1);
totalCount = IntegerUtils.createInteger(CollectionUtils.getFromUniqueCollection(countResult));
if (totalCount > 0) {
/** If record number is more than zero.
*/queryObject.setQueryProjections(backupProjections);
result = findListByQueryObject(queryObject, true, startIndex, maxResultCount);
}} else {
result = findListByQueryObject(queryObject, cacheable, startIndex, maxResultCount);
}return new Page<T>(startIndex, maxResultCount, totalCount, result);
}private List findListByQueryObject(final QueryObject queryObject, final boolean cacheable, final int startIndex, final int maxResultCount) {
// TODO aspectJ
HibernateDAOHelper.validateQueryObject(queryObject);
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = session.createCriteria(queryObject.getEntityClass());
if (cacheable) {
criteria.setCacheable(true);
if (getQueryCacheRegion() != null) {
criteria.setCacheRegion(getQueryCacheRegion());
}}criteria = getHelper().buildCriteria(criteria, queryObject);
if (maxResultCount > 0) {
criteria.setMaxResults(maxResultCount);
}if (startIndex > 0) {
criteria.setFirstResult(startIndex);
}return criteria.list();
}}, true);
}/*** Return an instance of Page which contains some persistent instances by especial query language.
** @param especial query language
* @param parameter's names
* @param parameter's values
* @param whether permit the use of caching
* @param starting index
* @param largest number of records
* @param pagination key
* @see com.bba96.core.util.Page
*/@SuppressWarnings("unchecked")
public Page findByExpQL(final String expql, final String[] names, final Object[] values, final boolean cacheable, final int startIndex, final int maxResultCount, final String paginationKey) {
Page result = new Page();
result.setPageSize(maxResultCount);
result.setStartIndex(startIndex);
if (paginationKey == null) {
result.setResults(findByExpQL(expql, names, values, cacheable, startIndex, maxResultCount));
} else {
int totalCount = (Integer) getByExpQL(HibernateDAOHelper.getCountSql(expql, paginationKey, null), names, values, cacheable);
result.setTotalCount(totalCount);
if (totalCount > 0) {
result.setResults(findByExpQL(expql, names, values, cacheable, startIndex, maxResultCount));
}}return result;
}@SuppressWarnings("unchecked")
private List findByExpQL(final String expql, final String[] names, final Object[] values, final boolean cacheable, final int startIndex, final int maxResultCount) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(expql);
if (cacheable) {
query.setCacheable(true);
if (getQueryCacheRegion() != null) {
query.setCacheRegion(getQueryCacheRegion());
}}if (names != null && values != null && names.length == values.length) {
for (int i = 0, max = names.length; i < max; i++) {
query.setParameter(names, values);
}}if (maxResultCount != -1) {
query.setMaxResults(maxResultCount);
}if (startIndex != -1) {
query.setFirstResult(startIndex);
}return query.list();
}}, true);
}/*** Return the number of the success which carried out by update or delete query language.
** @param especial query language
* @param parameter's names
* @param parameter's values
* @param whether permit the use of caching
* @return the number of the success
*/public int executeByExpQL(final String expql, final String[] names, final Object[] values, final boolean cacheable) {
return ((Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(expql);
if (cacheable) {
query.setCacheable(true);
if (getQueryCacheRegion() != null) {
query.setCacheRegion(getQueryCacheRegion());
}}if (names != null && values != null && names.length == values.length) {
for (int i = 0, max = names.length; i < max; i++) {
query.setParameter(names, values);
}}return query.executeUpdate();
}}, true)).intValue();
}/*** Return an instance which carried out by especial query language.
** @param especial query language
* @param parameter's names
* @param parameter's values
* @param whether permit the use of caching
* @return an instance
*/public Object getByExpQL(final String expql, final String[] names, final Object[] values, final boolean cacheable) {
return ((Object) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(expql);
if (cacheable) {
query.setCacheable(true);
if (getQueryCacheRegion() != null) {
query.setCacheRegion(getQueryCacheRegion());
}}if (names != null && values != null && names.length == values.length) {
for (int i = 0, max = names.length; i < max; i++) {
query.setParameter(names, values);
}}return query.uniqueResult();
}}, true));
}/*** Return an instance of Page which contains some persistent instances carried out by standard SQL.
** @param SQL
* @param alias
* @param persistent instance's class
* @param whether permit the use of caching
* @param starting index
* @param largest number of records
* @param pagination key
* @see com.bba96.core.util.Page
*/@SuppressWarnings("unchecked")
public Page findBySQL(final String sql, final String alias, final Class entityClass, final boolean cacheable, final int startIndex, final int maxResultCount, String paginationKey) {
Page result = new Page();
result.setPageSize(maxResultCount);
result.setStartIndex(startIndex);
if (paginationKey == null) {
result.setResults(findBySQL(sql, alias, entityClass, cacheable, startIndex, maxResultCount));
} else {
int totalCount = countBySQL(HibernateDAOHelper.getCountSql(sql, paginationKey, null), HibernateDAOHelper.COUNT_ALIAS);
result.setTotalCount(totalCount);
if (totalCount > 0) {
result.setResults(findBySQL(sql, alias, entityClass, cacheable, startIndex, maxResultCount));
}}return result;
}@SuppressWarnings("unchecked")
private List findBySQL(final String sql, final String alias, final Class entityClass, final boolean cacheable, final int startIndex, final int maxResultCount) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = (alias == null) ? session.createSQLQuery(sql).addEntity(entityClass) : session.createSQLQuery(sql).addEntity(alias, entityClass);
if (cacheable) {
query.setCacheable(true);
if (getQueryCacheRegion() != null) {
query.setCacheRegion(getQueryCacheRegion());
}}if (maxResultCount != -1) {
query.setMaxResults(maxResultCount);
}if (startIndex != -1) {
query.setFirstResult(startIndex);
}return query.list();
}}, true);
}/*** Return the number of the success which carried out by update or delete SQL.
** @param SQL
* @return the number of the success
*/public int executeBySQL(final String sql) {
return ((Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createSQLQuery(sql);
return query.executeUpdate();
}}, true)).intValue();
}/*** Return the number of the result which carried out by SQL with alias.
** @param SQL
* @param alias
* @return the number of the result
*/public int countBySQL(final String sql, final String alias) {
Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createSQLQuery(sql).addScalar(alias, Hibernate.INTEGER);
return query.uniqueResult();
}}, true);
return count == null ? 0 : count.intValue();
}/*** Return the instance which carried out by SQL with alias.
** @param SQL
* @param scalar
* @param type
* @return the instance
*/public Object findValueBySQL(final String sql, final String scalar, final Object type) {
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createSQLQuery(sql).addScalar(scalar, (Type) type);
return query.uniqueResult();
}}, true);
}}欢迎大家一起交流!我的QQ:185278122;邮箱:185278122@qq.com;无线搜索论坛网址:www.wxss.org;QQ群:26996585。
UID6 帖子3894 精华184
积分35672 财富65638 贡献12629 威望385 阅读权限20 性别男 来自北京 在线时间595 小时 注册时间2007-2-1 最后登录2008-11-3
查看个人网站
查看详细资料
TOP 上一主题
下一主题
控制面板首页
编辑个人资料
积分记录
公众用户组
升级个人空间
当前时区 GMT+8, 现在时间是 2009-1-9 18:57 粤ICP备07021541号
清除 Cookies
- 联系我们
- 无线搜索论坛
- Archiver
- WAP
- TOP
Powered by Discuz!
6.0.0 (C) 2001-2007 Comsenz Inc.
内页
内页
内页
直接浏览
提示:以上根据您的指令使用Timewe浏览服务访问的www.wxss.org网站,其内容、服务或立场跟Timewe无关
...
欢迎举报存在违法、不良信息的网站,净化网络环境
Wap推荐:
极品游戏大作