org.hibernate.query.criteria.internal.predicate.InPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
Hibernate's core ORM functionality
/*
* 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.predicate;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Subquery;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.ParameterRegistry;
import org.hibernate.query.criteria.internal.Renderable;
import org.hibernate.query.criteria.internal.ValueHandlerFactory;
import org.hibernate.query.criteria.internal.compile.RenderingContext;
import org.hibernate.query.criteria.internal.expression.LiteralExpression;
import org.hibernate.query.criteria.internal.expression.ParameterExpressionImpl;
import org.hibernate.type.Type;
/**
* Models a [NOT] IN restriction
*
* @author Steve Ebersole
*/
public class InPredicate
extends AbstractSimplePredicate
implements CriteriaBuilderImpl.In, Serializable {
private final Expression expression;
private final List> values;
/**
* Constructs an IN predicate against a given expression with an empty list of values.
*
* @param criteriaBuilder The query builder from which this originates.
* @param expression The expression.
*/
public InPredicate(
CriteriaBuilderImpl criteriaBuilder,
Expression expression) {
this( criteriaBuilder, expression, new ArrayList>() );
}
/**
* Constructs an IN predicate against a given expression with the given list of expression values.
*
* @param criteriaBuilder The query builder from which this originates.
* @param expression The expression.
* @param values The value list.
*/
public InPredicate(
CriteriaBuilderImpl criteriaBuilder,
Expression expression,
Expression... values) {
this( criteriaBuilder, expression, Arrays.asList( values ) );
}
/**
* Constructs an IN predicate against a given expression with the given list of expression values.
*
* @param criteriaBuilder The query builder from which this originates.
* @param expression The expression.
* @param values The value list.
*/
public InPredicate(
CriteriaBuilderImpl criteriaBuilder,
Expression expression,
List> values) {
super( criteriaBuilder );
this.expression = expression;
this.values = values;
}
/**
* Constructs an IN predicate against a given expression with the given given literal value list.
*
* @param criteriaBuilder The query builder from which this originates.
* @param expression The expression.
* @param values The value list.
*/
public InPredicate(
CriteriaBuilderImpl criteriaBuilder,
Expression expression,
T... values) {
this( criteriaBuilder, expression, Arrays.asList( values ) );
}
/**
* Constructs an IN predicate against a given expression with the given literal value list.
*
* @param criteriaBuilder The query builder from which this originates.
* @param expression The expression.
* @param values The value list.
*/
public InPredicate(
CriteriaBuilderImpl criteriaBuilder,
Expression expression,
Collection values) {
super( criteriaBuilder );
this.expression = expression;
this.values = new ArrayList>( values.size() );
final Class javaType = expression.getJavaType();
ValueHandlerFactory.ValueHandler valueHandler = javaType != null && ValueHandlerFactory.isNumeric(javaType)
? ValueHandlerFactory.determineAppropriateHandler((Class) javaType)
: new ValueHandlerFactory.NoOpValueHandler();
for ( T value : values ) {
this.values.add(
new LiteralExpression( criteriaBuilder, valueHandler.convert( value ) )
);
}
}
@Override
@SuppressWarnings("unchecked")
public Expression getExpression() {
return ( Expression ) expression;
}
public Expression getExpressionInternal() {
return expression;
}
public List> getValues() {
return values;
}
@Override
public InPredicate value(T value) {
return value( new LiteralExpression( criteriaBuilder(), value ) );
}
@Override
public InPredicate value(Expression value) {
values.add( value );
return this;
}
@Override
public void registerParameters(ParameterRegistry registry) {
Helper.possibleParameter( getExpressionInternal(), registry );
for ( Expression value : getValues() ) {
Helper.possibleParameter(value, registry);
}
}
@Override
public String render(boolean isNegated, RenderingContext renderingContext) {
final StringBuilder buffer = new StringBuilder();
final Expression exp = getExpression();
if ( ParameterExpressionImpl.class.isInstance( exp ) ) {
// technically we only need to CAST (afaik) if expression and all values are parameters.
// but checking for that condition could take long time on a lon value list
final ParameterExpressionImpl parameterExpression = (ParameterExpressionImpl) exp;
final SessionFactoryImplementor sfi = criteriaBuilder().getEntityManagerFactory().unwrap( SessionFactoryImplementor.class );
final Type mappingType = sfi.getTypeResolver().heuristicType( parameterExpression.getParameterType().getName() );
buffer.append( "cast(" )
.append( parameterExpression.render( renderingContext ) )
.append( " as " )
.append( mappingType.getName() )
.append( ")" );
}
else {
buffer.append( ( (Renderable) getExpression() ).render( renderingContext ) );
}
if ( isNegated ) {
buffer.append( " not" );
}
buffer.append( " in " );
// subquery expressions are already wrapped in parenthesis, so we only need to
// render the parenthesis here if the values represent an explicit value list
boolean isInSubqueryPredicate = getValues().size() == 1
&& Subquery.class.isInstance( getValues().get( 0 ) );
if ( isInSubqueryPredicate ) {
buffer.append( ( (Renderable) getValues().get(0) ).render( renderingContext ) );
}
else {
buffer.append( '(' );
String sep = "";
for ( Expression value : getValues() ) {
buffer.append( sep )
.append( ( (Renderable) value ).render( renderingContext ) );
sep = ", ";
}
buffer.append( ')' );
}
return buffer.toString();
}
}