All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.criterion.SubqueryExpression Maven / Gradle / Ivy

//$Id: SubqueryExpression.java 14209 2007-11-29 01:36:04Z gbadner $
package org.hibernate.criterion;

import java.util.HashMap;

import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.engine.QueryParameters;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.TypedValue;
import org.hibernate.impl.CriteriaImpl;
import org.hibernate.loader.criteria.CriteriaJoinWalker;
import org.hibernate.loader.criteria.CriteriaQueryTranslator;
import org.hibernate.persister.entity.OuterJoinLoadable;
import org.hibernate.type.Type;

/**
 * @author Gavin King
 */
public abstract class SubqueryExpression implements Criterion {
	
	private CriteriaImpl criteriaImpl;
	private String quantifier;
	private String op;
	private QueryParameters params;
	private Type[] types;
	private CriteriaQueryTranslator innerQuery;

	protected Type[] getTypes() {
		return types;
	}
	
	protected SubqueryExpression(String op, String quantifier, DetachedCriteria dc) {
		this.criteriaImpl = dc.getCriteriaImpl();
		this.quantifier = quantifier;
		this.op = op;
	}
	
	protected abstract String toLeftSqlString(Criteria criteria, CriteriaQuery outerQuery);

	public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
	throws HibernateException {

		final SessionFactoryImplementor factory = criteriaQuery.getFactory();
		final OuterJoinLoadable persister = (OuterJoinLoadable) factory.getEntityPersister( criteriaImpl.getEntityOrClassName() );

		createAndSetInnerQuery( criteriaQuery, factory );
		
		CriteriaJoinWalker walker = new CriteriaJoinWalker(
				persister,
				innerQuery,
				factory,
				criteriaImpl,
				criteriaImpl.getEntityOrClassName(),
				new HashMap(),
				innerQuery.getRootSQLALias());

		String sql = walker.getSQLString();

		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();
	}

	public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) 
	throws HibernateException {
		//the following two lines were added to ensure that this.params is not null, which
		//can happen with two-deep nested subqueries
		SessionFactoryImplementor factory = criteriaQuery.getFactory();
		createAndSetInnerQuery(criteriaQuery, factory);
		
		Type[] ppTypes = params.getPositionalParameterTypes();
		Object[] ppValues = params.getPositionalParameterValues();
		TypedValue[] tv = new TypedValue[ppTypes.length];
		for ( int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy