diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java index 5f2f8e5db..c0b6cd864 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java @@ -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 q = cb.createQuery(Customer.class); + Root c = q.from(Customer.class); + Parameter paramName = cb.parameter(String.class); + Parameter 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 query = em.createQuery(q); + query.setParameter(paramName, name); + query.setParameter(paramLastName, lastName); + + System.err.println("CQ: " + query.toString()); + + final List 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 cq = cb.createQuery(EntityWithIdClass.class); - Root c = cq.from(EntityWithIdClass.class); - em.createQuery(cq).getResultList(); + CriteriaQuery cq = cb.createQuery(EntityWithIdClass.class); + Root c = cq.from(EntityWithIdClass.class); + em.createQuery(cq).getResultList(); assertEquivalence(cq, jpql); } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java index 77b57d120..c748a0b7b 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java @@ -41,7 +41,7 @@ import org.apache.openjpa.util.InternalException; */ class ParameterExpressionImpl extends ExpressionImpl implements ParameterExpression, 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 extends ExpressionImpl */ public ParameterExpressionImpl(Class cls, String name) { super(cls); - if (name != null) + if (name != null) { assertValidName(name); + } + _name = name; } @@ -148,8 +150,12 @@ class ParameterExpressionImpl extends ExpressionImpl 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 extends ExpressionImpl 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(); - } }