mirror of https://github.com/apache/openjpa.git
OPENJPA-2788 fix anynomous Criteria Parameters
This commit is contained in:
parent
b29976342c
commit
00bd91cc2c
|
@ -26,6 +26,7 @@ import java.util.ArrayList;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.persistence.Parameter;
|
||||
import javax.persistence.Query;
|
||||
|
@ -580,6 +581,36 @@ public class TestTypesafeCriteria extends CriteriaTest {
|
|||
}, q, jpql);
|
||||
}
|
||||
|
||||
public void testParameters_wo_paramName() {
|
||||
final int rand = new Random().nextInt();
|
||||
final String name = "testName_" + rand;
|
||||
final String lastName = "lastName_" + rand;
|
||||
em.getTransaction().begin();
|
||||
Customer cNew = new Customer();
|
||||
cNew.setName(name);
|
||||
cNew.setLastName(lastName);
|
||||
cNew.setStatus(4711);
|
||||
em.persist(cNew);
|
||||
em.getTransaction().commit();
|
||||
|
||||
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
|
||||
Root<Customer> c = q.from(Customer.class);
|
||||
Parameter<String> paramName = cb.parameter(String.class);
|
||||
Parameter<String> paramLastName = cb.parameter(String.class);
|
||||
q.select(c).where(cb.and(cb.equal(c.get(Customer_.lastName), paramLastName),
|
||||
cb.equal(c.get(Customer_.name), paramName)));
|
||||
|
||||
final TypedQuery<Customer> query = em.createQuery(q);
|
||||
query.setParameter(paramName, name);
|
||||
query.setParameter(paramLastName, lastName);
|
||||
|
||||
System.err.println("CQ: " + query.toString());
|
||||
|
||||
final List<Customer> customers = query.getResultList();
|
||||
assertNotNull(customers);
|
||||
assertEquals(1, customers.size());
|
||||
}
|
||||
|
||||
public void testParameters3() {
|
||||
String jpql = "SELECT c FROM Customer c Where c.status = :stat";
|
||||
|
||||
|
@ -1608,10 +1639,9 @@ public class TestTypesafeCriteria extends CriteriaTest {
|
|||
|
||||
public void testIdClass() {
|
||||
String jpql = "select p from EntityWithIdClass p";
|
||||
|
||||
CriteriaQuery<EntityWithIdClass> cq = cb.createQuery(EntityWithIdClass.class);
|
||||
Root<EntityWithIdClass> c = cq.from(EntityWithIdClass.class);
|
||||
em.createQuery(cq).getResultList();
|
||||
CriteriaQuery<EntityWithIdClass> cq = cb.createQuery(EntityWithIdClass.class);
|
||||
Root<EntityWithIdClass> c = cq.from(EntityWithIdClass.class);
|
||||
em.createQuery(cq).getResultList();
|
||||
|
||||
assertEquivalence(cq, jpql);
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ import org.apache.openjpa.util.InternalException;
|
|||
*/
|
||||
class ParameterExpressionImpl<T> extends ExpressionImpl<T>
|
||||
implements ParameterExpression<T>, BindableParameter {
|
||||
private String _name;
|
||||
private final String _name;
|
||||
private int _index = 0; // index of the parameter as seen by the kernel, not position
|
||||
private Object value;
|
||||
|
||||
|
@ -53,8 +53,10 @@ class ParameterExpressionImpl<T> extends ExpressionImpl<T>
|
|||
*/
|
||||
public ParameterExpressionImpl(Class<T> cls, String name) {
|
||||
super(cls);
|
||||
if (name != null)
|
||||
if (name != null) {
|
||||
assertValidName(name);
|
||||
}
|
||||
|
||||
_name = name;
|
||||
}
|
||||
|
||||
|
@ -148,8 +150,12 @@ class ParameterExpressionImpl<T> extends ExpressionImpl<T>
|
|||
|
||||
ParameterExpressionImpl<?> that = (ParameterExpressionImpl<?>) o;
|
||||
|
||||
if (_name != null ? !_name.equals(that._name) : that._name != null)
|
||||
// we treat parameters the same ONLY if they are
|
||||
// * either the same instance (tested above)
|
||||
// * or have the same parameter name in the same tree
|
||||
if (_name == null || !_name.equals(that._name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if name is given, then we ignore the index
|
||||
if (_name == null && _index != that._index)
|
||||
|
@ -161,16 +167,4 @@ class ParameterExpressionImpl<T> extends ExpressionImpl<T>
|
|||
return value != null ? value.equals(that.value) : that.value == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue