package org.hibernate.jpa.criteria.predicate;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.criteria.Expression;
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.ParameterRegistry;
import org.hibernate.jpa.criteria.Renderable;
import org.hibernate.jpa.criteria.compile.RenderingContext;
import org.hibernate.jpa.criteria.expression.LiteralExpression;
import org.hibernate.jpa.criteria.path.PluralAttributePath;
public class MemberOfPredicate<E, C extends Collection<E>>
extends AbstractSimplePredicate
implements Serializable {
private final Expression<E> elementExpression;
private final PluralAttributePath<C> collectionPath;
public MemberOfPredicate(
CriteriaBuilderImpl criteriaBuilder,
Expression<E> elementExpression,
PluralAttributePath<C> collectionPath) {
super( criteriaBuilder );
this.elementExpression = elementExpression;
this.collectionPath = collectionPath;
}
public MemberOfPredicate(
CriteriaBuilderImpl criteriaBuilder,
E element,
PluralAttributePath<C> collectionPath) {
this(
criteriaBuilder,
new LiteralExpression<E>( criteriaBuilder, element ),
collectionPath
);
}
public PluralAttributePath<C> getCollectionPath() {
return collectionPath;
}
public Expression<E> getElementExpression() {
return elementExpression;
}
@Override
public void registerParameters(ParameterRegistry registry) {
Helper.possibleParameter( getCollectionPath(), registry );
Helper.possibleParameter( getElementExpression(), registry );
}
@Override
public String render(boolean isNegated, RenderingContext renderingContext) {
return ( (Renderable) elementExpression ).render( renderingContext )
+ ( isNegated ? " not" : "" ) + " member of "
+ getCollectionPath().render( renderingContext );
}
}