Skip to content

OPENJPA-2668 fix for query execution leaking into CriteriaQuery #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.openjpa.persistence.criteria;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
Expand All @@ -27,6 +29,7 @@
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.ParameterExpression;

import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.query.DomainObject;
Expand Down Expand Up @@ -646,5 +649,25 @@ void setParameters(Query q, Object...p) {
q.setParameter(p[i].toString(), p[i+1]);
}
}

public void testInCriteriaWithCollection() {

OpenJPAEntityManager em = emf.createEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();

CriteriaQuery<Customer> criteria = builder.createQuery(Customer.class);
Root<Customer> root = criteria.from(Customer.class);
criteria.where(root.get("status").in(builder.parameter(Collection.class)));

TypedQuery<Customer> query = em.createQuery(criteria);
System.err.println(query);
for (ParameterExpression parameter : criteria.getParameters()) {
query.setParameter(parameter, Arrays.asList(1L, 2L));
}

assertEquals("SELECT * FROM Customer c WHERE c.status IN (:param)", criteria.toString());
query.getResultList();
assertEquals("SELECT * FROM Customer c WHERE c.status IN (:param)", criteria.toString());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,12 @@ public In(Expression<?> e) {
this.e = (ExpressionImpl<T>)e;
}

private Expressions.In<?> copy() {
In<?> in = new Expressions.In<>(this.e);
in._exps.addAll(this._exps);
return in;
}

@Override
public Expression<T> getExpression() {
return e;
Expand Down Expand Up @@ -1468,7 +1474,11 @@ public PredicateImpl not() {
}

@Override
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
org.apache.openjpa.kernel.exps.Expression toKernelExpression(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
return copy().toKernelExpression0(factory, q);
}

org.apache.openjpa.kernel.exps.Expression toKernelExpression0(
ExpressionFactory factory, CriteriaQueryImpl<?> q) {
org.apache.openjpa.kernel.exps.Expression inExpr = null;
if (_exps.size() == 1) {
Expand Down