org.hibernate.impl.CriteriaImpl Java Examples

The following examples show how to use org.hibernate.impl.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: CriteriaQueryTranslator.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void createAssociationPathCriteriaMap() {
	Iterator iter = rootCriteria.iterateSubcriteria();
	while ( iter.hasNext() ) {
		CriteriaImpl.Subcriteria crit = ( CriteriaImpl.Subcriteria ) iter.next();
		String wholeAssociationPath = getWholeAssociationPath( crit );
		Object old = associationPathCriteriaMap.put( wholeAssociationPath, crit );
		if ( old != null ) {
			throw new QueryException( "duplicate association path: " + wholeAssociationPath );
		}
		int joinType = crit.getJoinType();
		old = associationPathJoinTypesMap.put( wholeAssociationPath, new Integer( joinType ) );
		if ( old != null ) {
			// TODO : not so sure this is needed...
			throw new QueryException( "duplicate association path: " + wholeAssociationPath );
		}
	}
}
 
Example #2
Source File: CriteriaQueryTranslator.java    From cacheonix-core with GNU Lesser General Public License v2.1 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;
	createAliasCriteriaMap();
	createAssociationPathCriteriaMap();
	createCriteriaEntityNameMap();
	createCriteriaSQLAliasMap();
}
 
Example #3
Source File: HibernateUtils.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * Return a existing alias for propertyPath on Criteria or null if none
 * @param criteria Hibernate Criteria
 * @param propertyPath the property path
 * @return alias or null if none
 */
public static String findAliasForPropertyPath(Criteria criteria, String propertyPath) {
	CriteriaImpl c = (CriteriaImpl) criteria;
	Iterator iter = c.iterateSubcriteria();
	while (iter.hasNext()) {
		Subcriteria subCriteria = (Subcriteria) iter.next();
		if (propertyPath.equals(subCriteria.getPath()));
			return subCriteria.getAlias();
	}
	// not found
	return null; 
}
 
Example #4
Source File: HibernateDao.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * Get Page, apply filter if any.
 * If Filter is a entity model, use Example to create a criteria.
 * else enable filter by name on session. 
 * @param page with page definitions
 * @return page of results
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <K> Page<K> getPage(Page<K> page) {
	
	List data = null;
	// try named query
	Query query = getQuery(page);
	if (query != null) {
		data = query.list();
	}
	else {
		// try filter, example and criteria builders
		Criteria criteria = getCriteria(page);
		ResultTransformer rt = ((CriteriaImpl) criteria).getResultTransformer(); 
		criteria.setProjection(Projections.rowCount());
		 page.setCount(((Long) criteria.uniqueResult()).intValue());
		// reset criteria
		criteria.setProjection(null);
		criteria.setResultTransformer(rt);
		// set start index and page size
		criteria.setFirstResult(page.getStartIndex())
			.setMaxResults(page.getPageSize());
		applyOrder(page, criteria);
		// run it
		criteria.setCacheable(cachePageQueries);
		data = criteria.list();
	}
	
	page.setData(data);
	
	return page;
}
 
Example #5
Source File: CriteriaQueryTranslator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String getOrderBy() {
	StringBuffer orderBy = new StringBuffer( 30 );
	Iterator criterionIterator = rootCriteria.iterateOrderings();
	while ( criterionIterator.hasNext() ) {
		CriteriaImpl.OrderEntry oe = ( CriteriaImpl.OrderEntry ) 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 cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String getWhereCondition() {
	StringBuffer condition = new StringBuffer( 30 );
	Iterator criterionIterator = rootCriteria.iterateExpressionEntries();
	while ( criterionIterator.hasNext() ) {
		CriteriaImpl.CriterionEntry entry = ( CriteriaImpl.CriterionEntry ) 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: SubqueryExpression.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
	
	final SessionImplementor session = ( (CriteriaImpl) criteria ).getSession(); //ugly!
	final SessionFactoryImplementor factory = session.getFactory();
	
	final OuterJoinLoadable persister = (OuterJoinLoadable) factory.getEntityPersister( criteriaImpl.getEntityOrClassName() );
	CriteriaQueryTranslator innerQuery = new CriteriaQueryTranslator( 
			factory, 
			criteriaImpl, 
			criteriaImpl.getEntityOrClassName(), //implicit polymorphism not supported (would need a union) 
			criteriaQuery.generateSQLAlias(),
			criteriaQuery
		);
	
	params = innerQuery.getQueryParameters(); //TODO: bad lifecycle....
	types = innerQuery.getProjectedTypes();
	
	//String filter = persister.filterFragment( innerQuery.getRootSQLALias(), session.getEnabledFilters() );
	
	String sql = new Select( factory.getDialect() )
		.setWhereClause( innerQuery.getWhereCondition() )
		.setGroupByClause( innerQuery.getGroupBy() )
		.setSelectClause( innerQuery.getSelect() )
		.setFromClause(
				persister.fromTableFragment( innerQuery.getRootSQLALias() ) +   
				persister.fromJoinFragment( innerQuery.getRootSQLALias(), true, false )
			)
		.toStatementString();
	
	final StringBuffer buf = new StringBuffer()
		.append( toLeftSqlString(criteria, criteriaQuery) );
	if (op!=null) buf.append(' ').append(op).append(' ');
	if (quantifier!=null) buf.append(quantifier).append(' ');
	return buf.append('(').append(sql).append(')')
		.toString();
}
 
Example #8
Source File: CriteriaQueryTranslator.java    From cacheonix-core with GNU Lesser General Public License v2.1 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 #9
Source File: CriteriaLoader.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public CriteriaLoader(
		final OuterJoinLoadable persister,
		final SessionFactoryImplementor factory,
		final CriteriaImpl criteria,
		final String rootEntityName,
		final Map enabledFilters)
throws HibernateException {
	super(factory, enabledFilters);

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

	querySpaces = translator.getQuerySpaces();

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

	initFromWalker(walker);

	userAliases = walker.getUserAliases();
	resultTypes = walker.getResultTypes();

	postInstantiate();

}
 
Example #10
Source File: CriteriaJoinWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public CriteriaJoinWalker(
		final OuterJoinLoadable persister, 
		final CriteriaQueryTranslator translator,
		final SessionFactoryImplementor factory, 
		final CriteriaImpl criteria, 
		final String rootEntityName,
		final Map enabledFilters)
throws HibernateException {
	super(persister, factory, enabledFilters);

	this.translator = translator;

	querySpaces = translator.getQuerySpaces();

	if ( translator.hasProjection() ) {
		resultTypes = translator.getProjectedTypes();
		
		initProjection( 
				translator.getSelect(), 
				translator.getWhereCondition(), 
				translator.getOrderBy(),
				translator.getGroupBy(),
				LockMode.NONE 
			);
	}
	else {
		resultTypes = new Type[] { TypeFactory.manyToOne( persister.getEntityName() ) };

		initAll( translator.getWhereCondition(), translator.getOrderBy(), LockMode.NONE );
	}
	
	userAliasList.add( criteria.getAlias() ); //root entity comes *last*
	userAliases = ArrayHelper.toStringArray(userAliasList);

}
 
Example #11
Source File: CriteriaQueryTranslator.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public CriteriaImpl getRootCriteria() {
	return rootCriteria;
}
 
Example #12
Source File: DetachedCriteria.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
CriteriaImpl getCriteriaImpl() {
	return impl;
}
 
Example #13
Source File: DetachedCriteria.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected DetachedCriteria(CriteriaImpl impl, Criteria criteria) {
	this.impl = impl;
	this.criteria = criteria;
}
 
Example #14
Source File: DetachedCriteria.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected DetachedCriteria(String entityName, String alias) {
	impl = new CriteriaImpl(entityName, alias, null);
	criteria = impl;
}
 
Example #15
Source File: DetachedCriteria.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected DetachedCriteria(String entityName) {
	impl = new CriteriaImpl(entityName, null);
	criteria = impl;
}
 
Example #16
Source File: SessionImplementor.java    From cacheonix-core with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Execute a criteria query
 */
public ScrollableResults scroll(CriteriaImpl criteria, ScrollMode scrollMode);
 
Example #17
Source File: SessionImplementor.java    From cacheonix-core with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Execute a criteria query
 */
public List list(CriteriaImpl criteria);