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

org.hibernate.query.criteria.internal.path.AbstractJoinImpl Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha1
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.query.criteria.internal.path;

import java.io.Serializable;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.metamodel.Attribute;

import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.CriteriaSubqueryImpl;
import org.hibernate.query.criteria.internal.FromImplementor;
import org.hibernate.query.criteria.internal.JoinImplementor;
import org.hibernate.query.criteria.internal.PathSource;
import org.hibernate.query.criteria.internal.compile.RenderingContext;
import org.hibernate.query.criteria.internal.predicate.AbstractPredicateImpl;

/**
 * Convenience base class for various {@link javax.persistence.criteria.Join} implementations.
 *
 * @author Steve Ebersole
 */
public abstract class AbstractJoinImpl
		extends AbstractFromImpl
		implements JoinImplementor, Serializable {

	private final Attribute joinAttribute;
	private final JoinType joinType;

	private Predicate suppliedJoinCondition;

	public AbstractJoinImpl(
			CriteriaBuilderImpl criteriaBuilder,
			PathSource pathSource,
			Attribute joinAttribute,
			JoinType joinType) {
		this( criteriaBuilder, joinAttribute.getJavaType(), pathSource, joinAttribute, joinType );
	}

	public AbstractJoinImpl(
			CriteriaBuilderImpl criteriaBuilder,
			Class javaType,
			PathSource pathSource,
			Attribute joinAttribute,
			JoinType joinType) {
		super( criteriaBuilder, javaType, pathSource );
		this.joinAttribute = joinAttribute;
		this.joinType = joinType;
	}

	@Override
	public Attribute getAttribute() {
		return joinAttribute;
	}

	@Override
	public JoinType getJoinType() {
		return joinType;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public From getParent() {
		// this cast should be ok by virtue of our constructors...
		return (From) getPathSource();
	}

	@Override
	public String renderTableExpression(RenderingContext renderingContext) {
		prepareAlias( renderingContext );
		( (FromImplementor) getParent() ).prepareAlias( renderingContext );
		StringBuilder tableExpression = new StringBuilder();
		tableExpression.append( getParent().getAlias() )
				.append( '.' )
				.append( getAttribute().getName() )
				.append( " as " )
				.append( getAlias() );
		if ( suppliedJoinCondition != null ) {
			tableExpression.append( " with " )
					.append( ( (AbstractPredicateImpl) suppliedJoinCondition ).render( renderingContext ) );
		}
		return tableExpression.toString();
	}

	@Override
	public JoinImplementor correlateTo(CriteriaSubqueryImpl subquery) {
		return (JoinImplementor) super.correlateTo( subquery );
	}

	@Override
	public JoinImplementor on(Predicate... restrictions) {
		// no matter what, a call to this method replaces any previously set values...
		this.suppliedJoinCondition = null;

		if ( restrictions != null && restrictions.length > 0 ) {
			this.suppliedJoinCondition = criteriaBuilder().and( restrictions );
		}

		return this;
	}

	@Override
	public JoinImplementor on(Expression restriction) {
		this.suppliedJoinCondition = criteriaBuilder().wrap( restriction );
		return this;
	}

	@Override
	public Predicate getOn() {
		return suppliedJoinCondition;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy