Java Code Examples for org.hibernate.impl.CriteriaImpl

The following examples show how to use org.hibernate.impl.CriteriaImpl. These examples are extracted from open source projects. 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
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
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 3
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 4
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 5
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 6
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 7
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 8
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 9
Source Project: jdal   Source File: HibernateDao.java    License: 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 10
Source Project: jdal   Source File: HibernateUtils.java    License: 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 11
protected DetachedCriteria(String entityName) {
	impl = new CriteriaImpl(entityName, null);
	criteria = impl;
}
 
Example 12
protected DetachedCriteria(String entityName, String alias) {
	impl = new CriteriaImpl(entityName, alias, null);
	criteria = impl;
}
 
Example 13
protected DetachedCriteria(CriteriaImpl impl, Criteria criteria) {
	this.impl = impl;
	this.criteria = criteria;
}
 
Example 14
CriteriaImpl getCriteriaImpl() {
	return impl;
}
 
Example 15
public CriteriaImpl getRootCriteria() {
	return rootCriteria;
}
 
Example 16
/**
 * Execute a criteria query
 */
public ScrollableResults scroll(CriteriaImpl criteria, ScrollMode scrollMode);
 
Example 17
/**
 * Execute a criteria query
 */
public List list(CriteriaImpl criteria);