mirror of https://github.com/apache/openjpa.git
OPENJPA-586 Query parameter binding for externalized fields
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@654272 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a31dfaa31f
commit
33e094c6f1
|
@ -1160,7 +1160,7 @@ public class JPQLExpressionBuilder
|
|||
if (fmd == null)
|
||||
return;
|
||||
|
||||
Class type = path.isXPath() ? path.getType() : fmd.getType();
|
||||
Class type = path.isXPath() ? path.getType() : fmd.getDeclaredType();
|
||||
if (type == null)
|
||||
return;
|
||||
|
||||
|
|
|
@ -19,14 +19,22 @@
|
|||
package org.apache.openjpa.persistence.meta;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.apache.openjpa.persistence.meta.common.apps.ExternalValues;
|
||||
import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
|
||||
import org.apache.openjpa.persistence.ArgumentException;
|
||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||
|
||||
/**
|
||||
* <p>Tests the {@link ExternalValuesFieldMapping}.</p>
|
||||
*
|
||||
* @author Abe White
|
||||
* @author Pinaki Poddar (added binding query parameter tests)
|
||||
*/
|
||||
public class TestExternalValues
|
||||
extends AbstractTestCase {
|
||||
|
@ -55,7 +63,6 @@ public class TestExternalValues
|
|||
pc.setDoubleToByte(4.5);
|
||||
pc.setCharToInt('f');
|
||||
pc.setStringToLong("foo");
|
||||
|
||||
pm.persist(pc);
|
||||
Object oid = pm.getObjectId(pc);
|
||||
endTx(pm);
|
||||
|
@ -112,4 +119,77 @@ public class TestExternalValues
|
|||
pc = (ExternalValues) pm.find(ExternalValues.class, oid);
|
||||
endEm(pm);
|
||||
}
|
||||
|
||||
public void testPositionalBindingQueryParameterEqualsDeclaredType() {
|
||||
UUID uuid = new UUID(1,4);
|
||||
createInstance(uuid);
|
||||
|
||||
EntityManager em = currentEntityManager();
|
||||
String jpql = "SELECT p FROM ExternalValues p WHERE p.uuid=?1";
|
||||
List<ExternalValues> result = em.createQuery(jpql)
|
||||
.setParameter(1, uuid)
|
||||
.getResultList();
|
||||
assertFalse(result.isEmpty());
|
||||
for (ExternalValues x:result) {
|
||||
assertEquals(uuid, x.getUuid());
|
||||
}
|
||||
}
|
||||
|
||||
public void testNamedBindingQueryParameterEqualsDeclaredType() {
|
||||
UUID uuid = new UUID(2,4);
|
||||
createInstance(uuid);
|
||||
|
||||
EntityManager em = currentEntityManager();
|
||||
String jpql = "SELECT p FROM ExternalValues p WHERE p.uuid=:uuid";
|
||||
List<ExternalValues> result = em.createQuery(jpql)
|
||||
.setParameter("uuid", uuid)
|
||||
.getResultList();
|
||||
assertFalse(result.isEmpty());
|
||||
for (ExternalValues pc:result) {
|
||||
assertEquals(uuid, pc.getUuid());
|
||||
}
|
||||
}
|
||||
|
||||
public void testPositionalBindingQueryParameterNotEqualsExternalizedType() {
|
||||
UUID uuid = new UUID(1,4);
|
||||
createInstance(uuid);
|
||||
|
||||
EntityManager em = currentEntityManager();
|
||||
String jpql = "SELECT p FROM ExternalValues p WHERE p.uuid=?1";
|
||||
Query query = em.createQuery(jpql)
|
||||
.setParameter(1, uuid.toString());
|
||||
|
||||
try {
|
||||
query.getResultList();
|
||||
fail("Expected ArgumentException");
|
||||
} catch (ArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testNamedBindingQueryParameterNotEqualsExternalizedType() {
|
||||
UUID uuid = new UUID(2,4);
|
||||
createInstance(uuid);
|
||||
|
||||
EntityManager em = currentEntityManager();
|
||||
String jpql = "SELECT p FROM ExternalValues p WHERE p.uuid=:uuid";
|
||||
Query query = em.createQuery(jpql)
|
||||
.setParameter("uuid", uuid.toString());
|
||||
try {
|
||||
query.getResultList();
|
||||
fail("Expected ArgumentException");
|
||||
} catch (ArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
private void createInstance(UUID uuid) {
|
||||
EntityManager em = currentEntityManager();
|
||||
em.getTransaction().begin();
|
||||
ExternalValues pc = new ExternalValues();
|
||||
pc.setUuid(uuid);
|
||||
em.persist(pc);
|
||||
em.getTransaction().commit();
|
||||
em.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,8 +19,14 @@
|
|||
package org.apache.openjpa.persistence.meta.common.apps;
|
||||
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
import org.apache.openjpa.persistence.Externalizer;
|
||||
import org.apache.openjpa.persistence.Factory;
|
||||
import org.apache.openjpa.persistence.Persistent;
|
||||
|
||||
@Entity
|
||||
public class ExternalValues {
|
||||
|
||||
|
@ -33,6 +39,10 @@ public class ExternalValues {
|
|||
private double doubleToByte;
|
||||
private char charToInt;
|
||||
private String stringToLong;
|
||||
@Persistent
|
||||
@Externalizer("toString")
|
||||
@Factory("UUID.fromString")
|
||||
private UUID uuid;
|
||||
|
||||
public boolean getBooleanToShort() {
|
||||
return booleanToShort;
|
||||
|
@ -105,4 +115,12 @@ public class ExternalValues {
|
|||
public void setStringToLong(String s) {
|
||||
stringToLong = s;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue