Java tutorial
package com.proper.uip.common.core.dao; /** * Copyright (c) 2005-2011 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: HibernateDao.java 1486 2011-02-12 15:50:51Z calvinxiu $ */ import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.criterion.CriteriaSpecification; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projection; import org.hibernate.criterion.Projections; import org.hibernate.internal.CriteriaImpl; import org.hibernate.transform.ResultTransformer; import org.hibernate.transform.Transformers; import com.proper.uip.common.utils.AssertUtils; import com.proper.uip.common.utils.Page; import com.proper.uip.common.utils.PageConfig; import com.proper.uip.common.utils.ReflectionUtils; /** * ?SpringSideHibernat DAO. * * ,?. * * @param <T> DAO? * @param <ID> * */ public class HibernateDao<T, ID extends Serializable> extends SimpleHibernateDao<T, ID> { /** * ??Class. * eg. * public class UserDao extends HibernateDao<User, Long>{ * } */ public HibernateDao() { super(); } public HibernateDao(Class<T> entityClass) { super(entityClass); } // -- --// /** * ?. */ public Page<T> getAll(PageConfig pageConfig) { return findPage(pageConfig); } /** * HQL. * * @param page ?. ???orderBy?. * @param hql hql?. * @param values ????,?. * * @return , ??. */ @SuppressWarnings("unchecked") public Page<T> findPage(PageConfig pageConfig, final String hql, final Object... values) { Page<T> page = new Page<T>(pageConfig); Query q = createQuery(hql, values); long totalCount = countHqlResult(hql, values); page.setTotal(totalCount); setPageParameterToQuery(q, page); List result = q.list(); page.setRows(result); return page; } @SuppressWarnings("unchecked") public <K> Page<K> findPage(PageConfig pageConfig, SqlEso eso, Class<K> clazz) { Page<K> page = new Page<K>(pageConfig); Query q = createQuery(eso.getSql(), eso.getParam()); long totalCount = countHqlResult(eso.getSql(), eso.getParam()); page.setTotal(totalCount); setPageParameterToQuery(q, page); q.setResultTransformer(Transformers.aliasToBean(clazz)); List result = q.list(); page.setRows(result); return page; } @SuppressWarnings("unchecked") public Page<T> findPage(PageConfig pageConfig, SqlEso eso) { Page<T> page = new Page<T>(pageConfig); Query q = createQuery(eso); long totalCount = countHqlResult(eso); page.setTotal(totalCount); setPageParameterToQuery(q, page); List result = q.list(); page.setRows(result); return page; } /** * HQL. * * @param page . * @param hql hql?. * @param values ????,?. * * @return , ??. */ @SuppressWarnings("unchecked") public Page<T> findAllPage(PageConfig pageConfig, final String hql, final Object... values) { Page<T> page = new Page<T>(pageConfig); Query q = createQuery(hql, values); long totalCount = countHqlResult(hql, values); page.setTotal(totalCount); setPageParameterToQuery(q, page); List result = q.list(); page.setRows(result); return page; } /** * HQL. * * @param page ?. ???orderBy?. * @param hql hql?. * @param values ???,??. * * @return , ??. */ @SuppressWarnings("unchecked") public Page<T> findPage(PageConfig pageConfig, final String hql, final Map<String, ?> values) { Page<T> page = new Page<T>(pageConfig); Query q = createQuery(hql, values); long totalCount = countHqlResult(hql, values); page.setTotal(totalCount); setPageParameterToQuery(q, page); List result = q.list(); page.setRows(result); return page; } private void setParams(Query query, Object[] values) { if (values != null) { for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } } } public SQLQuery createSQLQuery(String sql) { return this.getSession().createSQLQuery(sql); } @SuppressWarnings("unchecked") public <K> Page<K> pageSQLQuery(PageConfig pageConfig, SQLQuery sqlQuery, Object... values) { Page<K> page = new Page<K>(pageConfig); String totalSql = "select count(*) as total from(" + sqlQuery.getQueryString() + ")"; Query countQuery = this.getSession().createSQLQuery(totalSql); setParams(countQuery, values); Number total = (Number) countQuery.uniqueResult(); if (total != null) { page.setTotal(total.longValue()); sqlQuery.setFirstResult(page.getOffset()).setMaxResults(page.getPageSize()); setParams(sqlQuery, values); List<K> list = sqlQuery.list(); page.setRows(list); } else { page.setTotal(0); page.setRows(new ArrayList<K>()); } return page; } @SuppressWarnings("unchecked") public Page<Map<String, Object>> pageSQLQuery(PageConfig pageConfig, String sql, Object... values) { Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageConfig); String totalSql = "select count(*) as total from(" + sql + ")"; Query countQuery = this.getSession().createSQLQuery(totalSql); setParams(countQuery, values); Number total = (Number) countQuery.uniqueResult(); if (total != null) { page.setTotal(total.longValue()); Query query = this.getSession().createSQLQuery(sql).setFirstResult(page.getOffset()) .setMaxResults(page.getPageSize()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); setParams(query, values); List<Map<String, Object>> list = query.list(); page.setRows(list); } else { page.setTotal(0); page.setRows(new ArrayList<Map<String, Object>>()); } return page; } /** * ,?map,? * @param pageConfig * @param hsql * @param values * @return */ @SuppressWarnings("unchecked") public Page<Map<String, Object>> PageMapQuery(PageConfig pageConfig, String hsql, Map<String, Object> values) { Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageConfig); long total = countHqlResult(hsql, values); page.setTotal(total); Query query = this.createQuery(hsql, values).setFirstResult(page.getOffset()) .setMaxResults(page.getPageSize()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); page.setRows(query.list()); return page; } /** * Criteria. * * @param page ?. * @param criterions ???Criterion. * * @return .??. */ @SuppressWarnings("unchecked") public Page<T> findPage(PageConfig pageConfig, final Criterion... criterions) { Page<T> page = new Page<T>(pageConfig); Criteria c = createCriteria(criterions); long totalCount = countCriteriaResult(c); page.setTotal(totalCount); setPageParameterToCriteria(c, page); List result = c.list(); page.setRows(result); return page; } /** * ?Query,. */ protected Query setPageParameterToQuery(final Query q, final Page<?> page) { AssertUtils.isTrue(page.getPageSize() > 0, "Page Size must larger than zero"); q.setFirstResult(page.getOffset()); q.setMaxResults(page.getPageSize()); return q; } /** * ?Criteria,. */ protected Criteria setPageParameterToCriteria(final Criteria c, final Page<T> page) { AssertUtils.isTrue(page.getPageSize() > 0, "Page Size must larger than zero"); c.setFirstResult(page.getOffset()); c.setMaxResults(page.getPageSize()); if (page.isOrderBySetted()) { String[] orderByArray = StringUtils.split(page.getOrderBy(), ','); String[] orderArray = StringUtils.split(page.getOrder(), ','); AssertUtils.isTrue(orderByArray.length == orderArray.length, "???,????"); for (int i = 0; i < orderByArray.length; i++) { if (PageConfig.ASC.equals(orderArray[i])) { c.addOrder(Order.asc(orderByArray[i])); } else { c.addOrder(Order.desc(orderByArray[i])); } } } return c; } /** * countHql. * * ???hql?,??hql?count?. */ protected long countHqlResult(final String hql, final Object... values) { String countHql = prepareCountHql(hql); try { Long count = findUnique(countHql, values); return count; } catch (Exception e) { throw new RuntimeException("hql can't be auto count, hql is:" + countHql, e); } } protected long countHqlResult(final SqlEso eso) { String countHql = prepareCountHql(eso.getSql()); try { Long count = findUnique(countHql, eso.getParam()); return count; } catch (Exception e) { throw new RuntimeException("hql can't be auto count, hql is:" + countHql, e); } } /** * countHql. * * ???hql?,??hql?count?. */ protected long countHqlResult(final String hql, final Map<String, ?> values) { String countHql = prepareCountHql(hql); try { Long count = findUnique(countHql, values); return count; } catch (Exception e) { throw new RuntimeException("hql can't be auto count, hql is:" + countHql, e); } } private String prepareCountHql(String orgHql) { String fromHql = orgHql; // select??order by???count,?. fromHql = "from " + StringUtils.substringAfter(fromHql, "from"); fromHql = StringUtils.substringBefore(fromHql, "order by"); String countHql = "select count(*) " + fromHql; return countHql; } /** * countCriteria. */ @SuppressWarnings("unchecked") protected long countCriteriaResult(final Criteria c) { CriteriaImpl impl = (CriteriaImpl) c; // Projection?ResultTransformer?OrderBy??,??Count? Projection projection = impl.getProjection(); ResultTransformer transformer = impl.getResultTransformer(); List<CriteriaImpl.OrderEntry> orderEntries = null; try { orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries"); ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList()); } catch (Exception e) { logger.error("??:" + e.getMessage()); } // Count Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult(); long totalCount = (totalCountObject != null) ? totalCountObject : 0; // ?Projection,ResultTransformerOrderBy?? c.setProjection(projection); if (projection == null) { c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); } if (transformer != null) { c.setResultTransformer(transformer); } try { ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries); } catch (Exception e) { logger.error("??:" + e.getMessage()); } return totalCount; } // ------------------------------ /** * 1, 0<p> * ?:<p> * 1. mydao.findByHQuery(sql, arg1, arg2).fetch(); ? mydao.find(sql, arg1, arg2) ? * 2. mydao.findByHQuery(sql, arg1, arg2).fetch(100); ??100?, ?: [0-99] * 3. mydao.findByHQuery(sql, arg1, arg2).from(10).fetch(5); ?10?5?. ?: [10-14] * 4. mydao.findByHQuery(sql, arg1, arg2).fetch(3, 15); ? ?15 . ?: [30-44] * 5. mydao.findByHQuery(sql, arg1, arg2).from(10).fetch(3, 15); ?10? ?15 , ?: [40-54] */ public HQuery findByHQuery(String query, Object... params) { Query q = createQuery(query, params); return new HQuery(query, q); } public HQuery findByHQuery(Query q) { return new HQuery(q); } /** * */ public static class HQuery { public Query query; public String sq; private int from = 0; public HQuery(String sq, Query query) { this.query = query; this.sq = sq; } public HQuery(Query query) { this.query = query; this.sq = null; } @SuppressWarnings("unchecked") public <T> T first() { return (T) query.uniqueResult(); } public HQuery bind(String name, Object param) { if (param.getClass().isArray()) { param = Arrays.asList((Object[]) param); } query.setParameter(name, param); return this; } public HQuery bind(int position, Object param) { if (param.getClass().isArray()) { param = Arrays.asList((Object[]) param); } query.setParameter(position, param); return this; } /** * Retrieve all results of the query * @return A list of entities */ @SuppressWarnings("unchecked") public <T> List<T> fetch() { return query.list(); } @SuppressWarnings("unchecked") public <T> List<T> fetch(int max) { return query.setMaxResults(max).list(); } public <T> HQuery from(int position) { query.setFirstResult(position); this.from = position; return this; } @SuppressWarnings("unchecked") public <T> List<T> fetch(int page, int length) { if (page < 1) { page = 1; } this.from += (page - 1) * length; query.setFirstResult(this.from); query.setMaxResults(length); return query.list(); } } }