diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java index 1b1033164..bc65ec029 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java @@ -1095,7 +1095,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; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestExternalValues.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestExternalValues.java new file mode 100644 index 000000000..4dc99f4f9 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestExternalValues.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.meta; + + +import java.util.List; +import java.util.UUID; + +import javax.persistence.Query; + +import org.apache.openjpa.persistence.ArgumentException; +import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; +import org.apache.openjpa.persistence.meta.common.apps.ExternalValues; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +/** + *

Tests the {@link ExternalValuesFieldMapping}.

+ * + * @author Abe White + * @author Pinaki Poddar (added binding query parameter tests) + */ +public class TestExternalValues + extends SingleEMFTestCase { + + public void setUp() + throws Exception { + super.setUp(CLEAR_TABLES, ExternalValues.class); + } + + public void testPositionalBindingQueryParameterEqualsDeclaredType() { + UUID uuid = new UUID(1,4); + createInstance(uuid); + + OpenJPAEntityManagerSPI em = emf.createEntityManager(); + String jpql = "SELECT p FROM ExternalValues p WHERE p.uuid=?1"; + List 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); + + OpenJPAEntityManagerSPI em = emf.createEntityManager(); + String jpql = "SELECT p FROM ExternalValues p WHERE p.uuid=:uuid"; + List 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); + + OpenJPAEntityManagerSPI em = emf.createEntityManager(); + 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); + + OpenJPAEntityManagerSPI em = emf.createEntityManager(); + 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) { + OpenJPAEntityManagerSPI em = emf.createEntityManager(); + em.getTransaction().begin(); + ExternalValues pc = new ExternalValues(); + pc.setUuid(uuid); + em.persist(pc); + em.getTransaction().commit(); + em.clear(); + } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/common/apps/ExternalValues.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/common/apps/ExternalValues.java new file mode 100644 index 000000000..2e1a9997d --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/common/apps/ExternalValues.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +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 { + + private boolean booleanToShort; + private byte byteToDouble; + private int intToFloat; + private long longToChar; + private short shortToString; + private float floatToBoolean; + private double doubleToByte; + private char charToInt; + private String stringToLong; + @Persistent + @Externalizer("toString") + @Factory("UUID.fromString") + private UUID uuid; + + public boolean getBooleanToShort() { + return booleanToShort; + } + + public void setBooleanToShort(boolean b) { + booleanToShort = b; + } + + public byte getByteToDouble() { + return byteToDouble; + } + + public void setByteToDouble(byte b) { + byteToDouble = b; + } + + public int getIntToFloat() { + return intToFloat; + } + + public void setIntToFloat(int i) { + intToFloat = i; + } + + public long getLongToChar() { + return longToChar; + } + + public void setLongToChar(long l) { + longToChar = l; + } + + public short getShortToString() { + return shortToString; + } + + public void setShortToString(short s) { + shortToString = s; + } + + public double getDoubleToByte() { + return doubleToByte; + } + + public void setDoubleToByte(double d) { + doubleToByte = d; + } + + public float getFloatToBoolean() { + return floatToBoolean; + } + + public void setFloatToBoolean(float f) { + floatToBoolean = f; + } + + public char getCharToInt() { + return charToInt; + } + + public void setCharToInt(char c) { + charToInt = c; + } + + public String getStringToLong() { + return stringToLong; + } + + public void setStringToLong(String s) { + stringToLong = s; + } + + public UUID getUuid() { + return uuid; + } + + public void setUuid(UUID uuid) { + this.uuid = uuid; + } +}