org.hibernate.internal.CriteriaImpl Java Examples

The following examples show how to use org.hibernate.internal.CriteriaImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HibernateQuery.java    From gorm-hibernate5 with Apache License 2.0 6 votes vote down vote up
@Override
public Object clone() {
    final CriteriaImpl impl = (CriteriaImpl) criteria;
    final HibernateSession hibernateSession = (HibernateSession) getSession();
    final GrailsHibernateTemplate hibernateTemplate = (GrailsHibernateTemplate) hibernateSession.getNativeInterface();
    return hibernateTemplate.execute((GrailsHibernateTemplate.HibernateCallback<Object>) session -> {
        Criteria newCriteria = session.createCriteria(impl.getEntityOrClassName());

        Iterator iterator = impl.iterateExpressionEntries();
        while (iterator.hasNext()) {
            CriteriaImpl.CriterionEntry entry = (CriteriaImpl.CriterionEntry) iterator.next();
            newCriteria.add(entry.getCriterion());
        }
        Iterator subcriteriaIterator = impl.iterateSubcriteria();
        while (subcriteriaIterator.hasNext()) {
            CriteriaImpl.Subcriteria sub = (CriteriaImpl.Subcriteria) subcriteriaIterator.next();
            newCriteria.createAlias(sub.getPath(), sub.getAlias(), sub.getJoinType(), sub.getWithClause());
        }
        return new HibernateQuery(newCriteria, hibernateSession, entity);
    });
}
 
Example #2
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void createAssociationPathCriteriaMap() {
	final Iterator<CriteriaImpl.Subcriteria> iter = rootCriteria.iterateSubcriteria();
	while ( iter.hasNext() ) {
		CriteriaImpl.Subcriteria crit = iter.next();
		String wholeAssociationPath = getWholeAssociationPath( crit );
		Object old = associationPathCriteriaMap.put( wholeAssociationPath, crit );
		if ( old != null ) {
			throw new QueryException( "duplicate association path: " + wholeAssociationPath );
		}
		JoinType joinType = crit.getJoinType();
		old = associationPathJoinTypesMap.put( wholeAssociationPath, joinType );
		if ( old != null ) {
			// TODO : not so sure this is needed...
			throw new QueryException( "duplicate association path: " + wholeAssociationPath );
		}
		if ( crit.getWithClause() != null ) {
			this.withClauseMap.put( wholeAssociationPath, crit.getWithClause() );
		}
	}
}
 
Example #3
Source File: CriteriaJoinWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CriteriaJoinWalker(
		final OuterJoinLoadable persister,
		final CriteriaQueryTranslator translator,
		final SessionFactoryImplementor factory,
		final CriteriaImpl criteria,
		final String rootEntityName,
		final LoadQueryInfluencers loadQueryInfluencers,
		final String alias) {
	super( persister, factory, loadQueryInfluencers, alias );

	this.translator = translator;

	querySpaces = translator.getQuerySpaces();

	if ( translator.hasProjection() ) {
		initProjection(
				translator.getSelect(),
				translator.getWhereCondition(),
				translator.getOrderBy(),
				translator.getGroupBy(),
				LockOptions.NONE
		);
		resultTypes = translator.getProjectedTypes();
		userAliases = translator.getProjectedAliases();
		includeInResultRow = new boolean[resultTypes.length];
		Arrays.fill( includeInResultRow, true );
	}
	else {
		initAll( translator.getWhereCondition(), translator.getOrderBy(), LockOptions.NONE );
		// root entity comes last
		userAliasList.add( criteria.getAlias() ); //root entity comes *last*
		resultTypeList.add( translator.getResultType( criteria ) );
		includeInResultRowList.add( true );
		userAliases = ArrayHelper.toStringArray( userAliasList );
		resultTypes = ArrayHelper.toTypeArray( resultTypeList );
		includeInResultRow = ArrayHelper.toBooleanArray( includeInResultRowList );
	}
}
 
Example #4
Source File: PagedResultList.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
@Override
public int getTotalCount() {
    if (totalCount == Integer.MIN_VALUE) {
        totalCount = hibernateTemplate.execute(new GrailsHibernateTemplate.HibernateCallback<Integer>() {
            public Integer doInHibernate(Session session) throws HibernateException, SQLException {
                CriteriaImpl impl = (CriteriaImpl) criteria;
                Criteria totalCriteria = session.createCriteria(impl.getEntityOrClassName());
                hibernateTemplate.applySettings(totalCriteria);

                Iterator iterator = impl.iterateExpressionEntries();
                while (iterator.hasNext()) {
                    CriteriaImpl.CriterionEntry entry = (CriteriaImpl.CriterionEntry) iterator.next();
                    totalCriteria.add(entry.getCriterion());
                }
                Iterator subcriteriaIterator = impl.iterateSubcriteria();
                while (subcriteriaIterator.hasNext()) {
                    CriteriaImpl.Subcriteria sub = (CriteriaImpl.Subcriteria) subcriteriaIterator.next();
                    totalCriteria.createAlias(sub.getPath(), sub.getAlias(), sub.getJoinType(), sub.getWithClause());
                }
                totalCriteria.setProjection(impl.getProjection());
                totalCriteria.setProjection(Projections.rowCount());
                return ((Number)totalCriteria.uniqueResult()).intValue();
            }
        });
    }
    return totalCount;
}
 
Example #5
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public String getOrderBy() {
	StringBuilder orderBy = new StringBuilder( 30 );
	Iterator<CriteriaImpl.OrderEntry> criterionIterator = rootCriteria.iterateOrderings();
	while ( criterionIterator.hasNext() ) {
		CriteriaImpl.OrderEntry oe = criterionIterator.next();
		orderBy.append( oe.getOrder().toSqlString( oe.getCriteria(), this ) );
		if ( criterionIterator.hasNext() ) {
			orderBy.append( ", " );
		}
	}
	return orderBy.toString();
}
 
Example #6
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public String getWhereCondition() {
	StringBuilder condition = new StringBuilder( 30 );
	Iterator<CriteriaImpl.CriterionEntry> criterionIterator = rootCriteria.iterateExpressionEntries();
	while ( criterionIterator.hasNext() ) {
		CriteriaImpl.CriterionEntry entry = criterionIterator.next();
		String sqlString = entry.getCriterion().toSqlString( entry.getCriteria(), this );
		condition.append( sqlString );
		if ( criterionIterator.hasNext() ) {
			condition.append( " and " );
		}
	}
	return condition.toString();
}
 
Example #7
Source File: GenericBaseCommonDao.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 获取分页记录CriteriaQuery 老方法final int allCounts =
 * oConvertUtils.getInt(criteria
 * .setProjection(Projections.rowCount()).uniqueResult(), 0);
 * 
 * @param cq
 * @param isOffset
 * @return
 */
public PageList getPageList(final CriteriaQuery cq, final boolean isOffset) {

	Criteria criteria = cq.getDetachedCriteria().getExecutableCriteria(
			getSession());
	CriteriaImpl impl = (CriteriaImpl) criteria;
	// 先把Projection和OrderBy条件取出来,清空两者来执行Count操作
	Projection projection = impl.getProjection();
	final int allCounts = ((Long) criteria.setProjection(
			Projections.rowCount()).uniqueResult()).intValue();
	criteria.setProjection(projection);
	if (projection == null) {
		criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
	}

	// 判断是否有排序字段
	if (cq.getOrdermap() != null) {
		cq.setOrder(cq.getOrdermap());
	}
	int pageSize = cq.getPageSize();// 每页显示数
	int curPageNO = PagerUtil.getcurPageNo(allCounts, cq.getCurPage(),
			pageSize);// 当前页
	int offset = PagerUtil.getOffset(allCounts, curPageNO, pageSize);
	String toolBar = "";
	if (isOffset) {// 是否分页
		criteria.setFirstResult(offset);
		criteria.setMaxResults(cq.getPageSize());
		if (cq.getIsUseimage() == 1) {
			toolBar = PagerUtil.getBar(cq.getMyAction(), cq.getMyForm(),
					allCounts, curPageNO, pageSize, cq.getMap());
		} else {
			toolBar = PagerUtil.getBar(cq.getMyAction(), allCounts,
					curPageNO, pageSize, cq.getMap());
		}
	} else {
		pageSize = allCounts;
	}
	return new PageList(criteria.list(), toolBar, offset, curPageNO,
			allCounts);
}
 
Example #8
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private String getWholeAssociationPath(CriteriaImpl.Subcriteria subcriteria) {
	String path = subcriteria.getPath();

	// some messy, complex stuff here, since createCriteria() can take an
	// aliased path, or a path rooted at the creating criteria instance
	Criteria parent = null;
	if ( path.indexOf( '.' ) > 0 ) {
		// if it is a compound path
		String testAlias = StringHelper.root( path );
		if ( !testAlias.equals( subcriteria.getAlias() ) ) {
			// and the qualifier is not the alias of this criteria
			// -> check to see if we belong to some criteria other
			//  than the one that created us
			parent = aliasCriteriaMap.get( testAlias );
		}
	}
	if ( parent == null ) {
		// otherwise assume the parent is the the criteria that created us
		parent = subcriteria.getParent();
	}
	else {
		path = StringHelper.unroot( path );
	}

	if ( parent.equals( rootCriteria ) ) {
		// if its the root criteria, we are done
		return path;
	}
	else {
		// otherwise, recurse
		return getWholeAssociationPath( ( CriteriaImpl.Subcriteria ) parent ) + '.' + path;
	}
}
 
Example #9
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void createAliasCriteriaMap() {
	aliasCriteriaMap.put( rootCriteria.getAlias(), rootCriteria );
	Iterator<CriteriaImpl.Subcriteria> iter = rootCriteria.iterateSubcriteria();
	while ( iter.hasNext() ) {
		Criteria subcriteria = iter.next();
		if ( subcriteria.getAlias() != null ) {
			Object old = aliasCriteriaMap.put( subcriteria.getAlias(), subcriteria );
			if ( old != null ) {
				throw new QueryException( "duplicate alias: " + subcriteria.getAlias() );
			}
		}
	}
}
 
Example #10
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CriteriaQueryTranslator(
		final SessionFactoryImplementor factory,
		final CriteriaImpl criteria,
		final String rootEntityName,
		final String rootSQLAlias) throws HibernateException {
	this.rootCriteria = criteria;
	this.rootEntityName = rootEntityName;
	this.sessionFactory = factory;
	this.rootSQLAlias = rootSQLAlias;
	this.helper = new SessionFactoryHelper(factory);
	createAliasCriteriaMap();
	createAssociationPathCriteriaMap();
	createCriteriaEntityNameMap();
	createCriteriaSQLAliasMap();
}
 
Example #11
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CriteriaQueryTranslator(
		final SessionFactoryImplementor factory,
		final CriteriaImpl criteria,
		final String rootEntityName,
		final String rootSQLAlias,
		CriteriaQuery outerQuery) throws HibernateException {
	this( factory, criteria, rootEntityName, rootSQLAlias );
	outerQueryTranslator = outerQuery;
}
 
Example #12
Source File: CriteriaLoader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CriteriaLoader(
		final OuterJoinLoadable persister,
		final SessionFactoryImplementor factory,
		final CriteriaImpl criteria,
		final String rootEntityName,
		final LoadQueryInfluencers loadQueryInfluencers) throws HibernateException {
	super( factory, loadQueryInfluencers );

	translator = new CriteriaQueryTranslator(
			factory,
			criteria,
			rootEntityName,
			CriteriaQueryTranslator.ROOT_SQL_ALIAS
		);

	querySpaces = translator.getQuerySpaces();

	CriteriaJoinWalker walker = new CriteriaJoinWalker(
			persister,
			translator,
			factory,
			criteria,
			rootEntityName,
			loadQueryInfluencers
		);

	initFromWalker(walker);

	userAliases = walker.getUserAliases();
	resultTypes = walker.getResultTypes();
	includeInResultRow = walker.includeInResultRow();
	resultRowLength = ArrayHelper.countTrue( includeInResultRow );

	postInstantiate();

}
 
Example #13
Source File: CriteriaJoinWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CriteriaJoinWalker(
		final OuterJoinLoadable persister,
		final CriteriaQueryTranslator translator,
		final SessionFactoryImplementor factory,
		final CriteriaImpl criteria,
		final String rootEntityName,
		final LoadQueryInfluencers loadQueryInfluencers) {
	this( persister, translator, factory, criteria, rootEntityName, loadQueryInfluencers, null );
}
 
Example #14
Source File: GenericBaseCommonDao.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 返回JQUERY datatables DataTableReturn模型对象
 */
public DataTableReturn getDataTableReturn(final CriteriaQuery cq,
		final boolean isOffset) {

	Criteria criteria = cq.getDetachedCriteria().getExecutableCriteria(
			getSession());
	CriteriaImpl impl = (CriteriaImpl) criteria;
	// 先把Projection和OrderBy条件取出来,清空两者来执行Count操作
	Projection projection = impl.getProjection();
	final int allCounts = ((Long) criteria.setProjection(
			Projections.rowCount()).uniqueResult()).intValue();
	criteria.setProjection(projection);
	if (projection == null) {
		criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
	}

	// 判断是否有排序字段
	if (cq.getOrdermap() != null) {
		cq.setOrder(cq.getOrdermap());
	}
	int pageSize = cq.getPageSize();// 每页显示数
	int curPageNO = PagerUtil.getcurPageNo(allCounts, cq.getCurPage(),
			pageSize);// 当前页
	int offset = PagerUtil.getOffset(allCounts, curPageNO, pageSize);
	if (isOffset) {// 是否分页
		criteria.setFirstResult(offset);
		criteria.setMaxResults(cq.getPageSize());
	} else {
		pageSize = allCounts;
	}
	DetachedCriteriaUtil.selectColumn(cq.getDetachedCriteria(), cq
			.getField().split(","), cq.getEntityClass(), false);
	return new DataTableReturn(allCounts, allCounts, cq.getDataTables()
			.getEcho(), criteria.list());
}
 
Example #15
Source File: GenericBaseCommonDao.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 返回easyui datagrid DataGridReturn模型对象
 */
public DataGridReturn getDataGridReturn(final CriteriaQuery cq,
		final boolean isOffset) {
	Criteria criteria = cq.getDetachedCriteria().getExecutableCriteria(
			getSession());
	CriteriaImpl impl = (CriteriaImpl) criteria;
	// 先把Projection和OrderBy条件取出来,清空两者来执行Count操作
	Projection projection = impl.getProjection();
	final int allCounts = ((Long) criteria.setProjection(
			Projections.rowCount()).uniqueResult()).intValue();
	criteria.setProjection(projection);
	if (projection == null) {
		criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
	}
	if (StringUtils.isNotBlank(cq.getDataGrid().getSort())) {
		cq.addOrder(cq.getDataGrid().getSort(), cq.getDataGrid().getOrder());
	}

	// 判断是否有排序字段
	if (!cq.getOrdermap().isEmpty()) {
		cq.setOrder(cq.getOrdermap());
	}
	int pageSize = cq.getPageSize();// 每页显示数
	int curPageNO = PagerUtil.getcurPageNo(allCounts, cq.getCurPage(),
			pageSize);// 当前页
	int offset = PagerUtil.getOffset(allCounts, curPageNO, pageSize);
	if (isOffset) {// 是否分页
		criteria.setFirstResult(offset);
		criteria.setMaxResults(cq.getPageSize());
	} else {
		pageSize = allCounts;
	}
	// DetachedCriteriaUtil.selectColumn(cq.getDetachedCriteria(),
	// cq.getField().split(","), cq.getClass1(), false);
	List list = criteria.list();
	cq.getDataGrid().setResults(list);
	cq.getDataGrid().setTotal(allCounts);
	return new DataGridReturn(allCounts, list);
}
 
Example #16
Source File: BaseHibernateDao.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param detachedCriteria
 * @param pi
 * @param pageSize
 * @param <T> T
 * @return T
 * @throws DaoException <br>
 */
@Override
public <T> PagerList<T> getPageList(final DetachedCriteria detachedCriteria, final int pi, final int pageSize)
    throws DaoException {
    int pageIndex = pi;
    if (pi == 0) {
        pageIndex = 1;
    }

    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());

    // 查询分页总数
    CriteriaImpl impl = (CriteriaImpl) criteria;
    Projection projection = impl.getProjection();
    Long allCounts = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();

    criteria.setProjection(projection);
    criteria.setFirstResult((pageIndex - 1) * pageSize);
    criteria.setMaxResults(pageSize);

    PagerList<T> resultList = new PagerList<T>();
    resultList.setPageIndex(pageIndex);
    resultList.setPageSize(pageSize);
    if (allCounts == null) {
        allCounts = 0L;
    }
    resultList.setTotalCount(allCounts);

    if (allCounts > 0) {
        resultList.addAll(criteria.list());
    }
    return resultList;
}
 
Example #17
Source File: HibernateUtils.java    From lemon with Apache License 2.0 5 votes vote down vote up
/**
 * find projection from criteria.
 * 
 * @param criteria
 *            Criteria
 * @return Projection
 */
public static Projection findProjection(Criteria criteria) {
    if (criteria instanceof CriteriaImpl) {
        return ((CriteriaImpl) criteria).getProjection();
    } else {
        throw new IllegalArgumentException(criteria
                + " is not a CriteriaImpl");
    }
}
 
Example #18
Source File: SubqueryExpression.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private SharedSessionContractImplementor deriveRootSession(Criteria criteria) {
	if ( criteria instanceof CriteriaImpl ) {
		return ( (CriteriaImpl) criteria ).getSession();
	}
	else if ( criteria instanceof CriteriaImpl.Subcriteria ) {
		return deriveRootSession( ( (CriteriaImpl.Subcriteria) criteria ).getParent() );
	}
	else {
		// could happen for custom Criteria impls.  Not likely, but...
		// 		for long term solution, see HHH-3514
		return null;
	}
}
 
Example #19
Source File: HibernatePagingDao.java    From lemon with Apache License 2.0 5 votes vote down vote up
/**
 * 分页查询函数,使用已设好查询条件与排序的<code>Criteria</code>.
 * 
 * @param criteria
 *            条件
 * @param pageNo
 *            当前页号
 * @param pageSize
 *            每页最大记录数
 * @return 含总记录数和当前页数据的Page对象.
 */
@Transactional(readOnly = true)
public Page pagedQuery(Criteria criteria, int pageNo, int pageSize) {
    Assert.notNull(criteria);
    Assert.isTrue(pageNo >= 1, "pageNo should be eg 1");
    Assert.isTrue(criteria instanceof CriteriaImpl);

    // 先把Projection和OrderBy条件取出来,清空两者来执行Count操作
    Projection projection = HibernateUtils.findProjection(criteria);

    List orderEntries = HibernateUtils.findOrderEntries(criteria);
    HibernateUtils.setOrderEntries(criteria, Collections.EMPTY_LIST);

    // 执行查询
    Integer totalCount = this.getCount(criteria);
    // 将之前的Projection和OrderBy条件重新设回去
    criteria.setProjection(projection);

    if (projection == null) {
        criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }

    HibernateUtils.setOrderEntries(criteria, orderEntries);

    // 返回分页对象
    if (totalCount < 1) {
        return new Page();
    }

    int start = (pageNo - 1) * pageSize;
    List result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .list();

    Page page = new Page(result, totalCount);
    page.setPageNo(pageNo);
    page.setPageSize(pageSize);

    return page;
}
 
Example #20
Source File: GenericBaseCommonDao.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 获取分页记录CriteriaQuery 老方法final int allCounts =
 * oConvertUtils.getInt(criteria
 * .setProjection(Projections.rowCount()).uniqueResult(), 0);
 *
 * @param cq
 * @param isOffset
 * @return
 */
public PageList getPageList(final CriteriaQuery cq, final boolean isOffset) {

	Criteria criteria = cq.getDetachedCriteria().getExecutableCriteria(
			getSession());
	CriteriaImpl impl = (CriteriaImpl) criteria;
	// 先把Projection和OrderBy条件取出来,清空两者来执行Count操作
	Projection projection = impl.getProjection();
	final int allCounts = ((Long) criteria.setProjection(
			Projections.rowCount()).uniqueResult()).intValue();
	criteria.setProjection(projection);
	if (projection == null) {
		criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
	}

	// 判断是否有排序字段
	if (cq.getOrdermap() != null) {
		cq.setOrder(cq.getOrdermap());
	}
	int pageSize = cq.getPageSize();// 每页显示数
	int curPageNO = PagerUtil.getcurPageNo(allCounts, cq.getCurPage(),
			pageSize);// 当前页
	int offset = PagerUtil.getOffset(allCounts, curPageNO, pageSize);
	String toolBar = "";
	if (isOffset) {// 是否分页
		criteria.setFirstResult(offset);
		criteria.setMaxResults(cq.getPageSize());
		if (cq.getIsUseimage() == 1) {
			toolBar = PagerUtil.getBar(cq.getMyAction(), cq.getMyForm(),
					allCounts, curPageNO, pageSize, cq.getMap());
		} else {
			toolBar = PagerUtil.getBar(cq.getMyAction(), allCounts,
					curPageNO, pageSize, cq.getMap());
		}
	} else {
		pageSize = allCounts;
	}
	return new PageList(criteria.list(), toolBar, offset, curPageNO,
			allCounts);
}
 
Example #21
Source File: GenericBaseCommonDao.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 返回JQUERY datatables DataTableReturn模型对象
 */
public DataTableReturn getDataTableReturn(final CriteriaQuery cq,
		final boolean isOffset) {

	Criteria criteria = cq.getDetachedCriteria().getExecutableCriteria(
			getSession());
	CriteriaImpl impl = (CriteriaImpl) criteria;
	// 先把Projection和OrderBy条件取出来,清空两者来执行Count操作
	Projection projection = impl.getProjection();
	final int allCounts = ((Long) criteria.setProjection(
			Projections.rowCount()).uniqueResult()).intValue();
	criteria.setProjection(projection);
	if (projection == null) {
		criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
	}

	// 判断是否有排序字段
	if (cq.getOrdermap() != null) {
		cq.setOrder(cq.getOrdermap());
	}
	int pageSize = cq.getPageSize();// 每页显示数
	int curPageNO = PagerUtil.getcurPageNo(allCounts, cq.getCurPage(),
			pageSize);// 当前页
	int offset = PagerUtil.getOffset(allCounts, curPageNO, pageSize);
	if (isOffset) {// 是否分页
		criteria.setFirstResult(offset);
		criteria.setMaxResults(cq.getPageSize());
	} else {
		pageSize = allCounts;
	}

	//DetachedCriteriaUtil.selectColumn(cq.getDetachedCriteria(), cq.getField().split(","), cq.getEntityClass(), false);

	
	return new DataTableReturn(allCounts, allCounts, cq.getDataTables().getEcho(), criteria.list());
}
 
Example #22
Source File: GenericBaseCommonDao.java    From jeecg with Apache License 2.0 4 votes vote down vote up
/**
	 * 返回easyui datagrid DataGridReturn模型对象
	 */

	public void getDataGridReturn(CriteriaQuery cq,final boolean isOffset) {

		Criteria criteria = cq.getDetachedCriteria().getExecutableCriteria(getSession());
		CriteriaImpl impl = (CriteriaImpl) criteria;
		// 先把Projection和OrderBy条件取出来,清空两者来执行Count操作
		Projection projection = impl.getProjection();

//		final int allCounts = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
		Object allCountsObj = criteria.setProjection(Projections.rowCount()).uniqueResult();
		final int allCounts;
		if(allCountsObj==null){
			allCounts = 0;
		}else{
			allCounts = ((Long) allCountsObj).intValue();
		}

		criteria.setProjection(projection);
		if (projection == null) {
			criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
		}

		Map<String, Object> ordermap = cq.getOrdermap();
		if(ordermap==null){
			ordermap = new LinkedHashMap<String, Object>();
		}
		
		String sort = cq.getDataGrid().getSort();
		if (StringUtils.isNotBlank(sort)) {
			String []sortArr = sort.split(",");
			String []orderArr = cq.getDataGrid().getOrder().split(",");
			if(sortArr.length != orderArr.length && orderArr.length > 0){
				for (int i = 0; i < sortArr.length; i++) {
					if(SortDirection.asc.equals(SortDirection.toEnum(orderArr[0]))){
						ordermap.put(sortArr[i], SortDirection.asc);
					}else{
						ordermap.put(sortArr[i], SortDirection.desc);
					}
				}
			}else if(sortArr.length == orderArr.length){
				for (int i = 0; i < sortArr.length; i++) {
					if(SortDirection.asc.equals(SortDirection.toEnum(orderArr[i]))){
						ordermap.put(sortArr[i], SortDirection.asc);
					}else{
						ordermap.put(sortArr[i], SortDirection.desc);
					}
				}
			}
		}
		if(!ordermap.isEmpty() && ordermap.size()>0){
			cq.setOrder(ordermap);
		}


		// 判断是否有排序字段
//		if (!cq.getOrdermap().isEmpty()) {
//			cq.setOrder(cq.getOrdermap());
//		}
		int pageSize = cq.getPageSize();// 每页显示数
		int curPageNO = PagerUtil.getcurPageNo(allCounts, cq.getCurPage(),pageSize);// 当前页
		int offset = PagerUtil.getOffset(allCounts, curPageNO, pageSize);
		if (isOffset) {// 是否分页
			criteria.setFirstResult(offset);
			criteria.setMaxResults(cq.getPageSize());
		} else {
			pageSize = allCounts;
		}
		// DetachedCriteriaUtil.selectColumn(cq.getDetachedCriteria(),
		// cq.getField().split(","), cq.getClass1(), false);
		List<?> list = criteria.list();
		cq.getDataGrid().setResults(list);
		cq.getDataGrid().setTotal(allCounts);

		cq.clear();
		cq = null;

		//return new DataGridReturn(allCounts, list);

	}
 
Example #23
Source File: EntityCriteria.java    From onedev with MIT License 4 votes vote down vote up
protected EntityCriteria(String entityName) {
	impl = new CriteriaImpl(entityName, null);
	criteria = impl;
}
 
Example #24
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public boolean hasRestriction(String path) {
	final CriteriaImpl.Subcriteria subcriteria = (CriteriaImpl.Subcriteria) getCriteria( path );
	return subcriteria != null && subcriteria.hasRestriction();
}
 
Example #25
Source File: CriteriaQueryTranslator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public CriteriaImpl getRootCriteria() {
	return rootCriteria;
}
 
Example #26
Source File: DetachedCriteria.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected DetachedCriteria(CriteriaImpl impl, Criteria criteria) {
	this.impl = impl;
	this.criteria = criteria;
}
 
Example #27
Source File: DetachedCriteria.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected DetachedCriteria(String entityName, String alias) {
	impl = new CriteriaImpl( entityName, alias, null );
	criteria = impl;
}
 
Example #28
Source File: DetachedCriteria.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected DetachedCriteria(String entityName) {
	impl = new CriteriaImpl( entityName, null );
	criteria = impl;
}
 
Example #29
Source File: EntityCriteria.java    From onedev with MIT License 4 votes vote down vote up
CriteriaImpl getCriteriaImpl() {
	return impl;
}
 
Example #30
Source File: EntityCriteria.java    From onedev with MIT License 4 votes vote down vote up
/**
 * Get an executable instance of <literal>Criteria</literal>,
 * to actually run the query.
 */
public Criteria getExecutableCriteria(Session session) {
	CriteriaImpl clone = (CriteriaImpl) SerializationUtils.clone(impl);
	clone.setSession( ( SessionImplementor ) session );
	return clone;
}