You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pp...@apache.org on 2009/03/06 08:50:54 UTC
svn commit: r750798 - in /openjpa/trunk:
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/
openjpa-kernel/src/main/java/org/apache/openjpa/kernel/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/
openjpa-project/src...
Author: ppoddar
Date: Fri Mar 6 07:50:54 2009
New Revision: 750798
URL: http://svn.apache.org/viewvc?rev=750798&view=rev
Log:
OPENJPA-955: test+documentation on MethodQL. Allow candidate class be optional on MethodQL. Do not cache MethodQLQuery.
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/MethodStoreQuery.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestMethodQLQuery.java
openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_runtime.xml
Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java?rev=750798&r1=750797&r2=750798&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java Fri Mar 6 07:50:54 2009
@@ -72,6 +72,7 @@
if (id == null
|| query == null
|| QueryLanguages.LANG_SQL.equals(query.getLanguage())
+ || QueryLanguages.LANG_METHODQL.equals(query.getLanguage())
|| isHinted(hints, QueryHints.HINT_IGNORE_PREPARED_QUERY)
|| isHinted(hints, QueryHints.HINT_INVALIDATE_PREPARED_QUERY))
return Boolean.FALSE;
Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/MethodStoreQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/MethodStoreQuery.java?rev=750798&r1=750797&r2=750798&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/MethodStoreQuery.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/MethodStoreQuery.java Fri Mar 6 07:50:54 2009
@@ -88,6 +88,10 @@
return true;
}
+ public boolean requiresCandidateType() {
+ return false;
+ }
+
/**
* Parse the parameter declarations.
*/
Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestMethodQLQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestMethodQLQuery.java?rev=750798&r1=750797&r2=750798&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestMethodQLQuery.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestMethodQLQuery.java Fri Mar 6 07:50:54 2009
@@ -18,20 +18,29 @@
*/
package org.apache.openjpa.persistence.query;
+import java.util.Collections;
import java.util.Map;
-import java.util.ArrayList;
-import org.apache.openjpa.persistence.test.SingleEMTestCase;
-import org.apache.openjpa.persistence.OpenJPAQuery;
-import org.apache.openjpa.persistence.QueryImpl;
-import org.apache.openjpa.persistence.ArgumentException;
+import org.apache.openjpa.kernel.FetchConfiguration;
+import org.apache.openjpa.kernel.Query;
import org.apache.openjpa.kernel.QueryLanguages;
import org.apache.openjpa.kernel.StoreContext;
-import org.apache.openjpa.kernel.FetchConfiguration;
-import org.apache.openjpa.meta.ClassMetaData;
-import org.apache.openjpa.lib.rop.ResultObjectProvider;
import org.apache.openjpa.lib.rop.ListResultObjectProvider;
+import org.apache.openjpa.lib.rop.ResultObjectProvider;
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.persistence.ArgumentException;
+import org.apache.openjpa.persistence.OpenJPAQuery;
+import org.apache.openjpa.persistence.QueryImpl;
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
+/**
+ * Tests MethodQL.
+ *
+ * The 'user method' is simply echos the parameters set on the query.
+ *
+ * @author Pinaki Poddar
+ *
+ */
public class TestMethodQLQuery
extends SingleEMTestCase {
@@ -40,21 +49,32 @@
setUp(SimpleEntity.class);
}
+ OpenJPAQuery createMethodQuery(String method) {
+ String methodName = getClass().getName()+ "." + method;
+ return em.createQuery(QueryLanguages.LANG_METHODQL, methodName);
+ }
+
+ public void testMethodQLWithParameters() {
+ OpenJPAQuery q = createMethodQuery("echo");
+ Query kernelQ = q.unwrap(Query.class);
+ kernelQ.declareParameters("String firstName, String lastName");
+ q.setParameter("firstName", "Fred").setParameter("lastName", "Lucas");
+ Object result = q.getResultList().get(0);
+ assertTrue(result instanceof Map);
+ Map params = (Map)result;
+ assertEquals("Fred", params.get("firstName"));
+ assertEquals("Lucas", params.get("lastName"));
+ }
+
public void testMethodQLWithoutParametersDeclared() {
- OpenJPAQuery q = em.createQuery(QueryLanguages.LANG_METHODQL,
- getClass().getName() + ".echo");
- ((QueryImpl) q).getDelegate().setCandidateType(
- SimpleEntity.class, true);
- q.setParameter("param", 5);
- ((QueryImpl) q).getDelegate().declareParameters("Integer param");
- assertEquals(5, q.getResultList().get(0));
+ OpenJPAQuery q = createMethodQuery("echo");
+ Object result = q.getResultList().get(0);
+ assertTrue(result instanceof Map);
+ assertTrue(((Map)result).isEmpty());
}
public void testInvalidMethodReturnType() {
- OpenJPAQuery q = em.createQuery(QueryLanguages.LANG_METHODQL,
- getClass().getName() + ".invalidReturnMeth");
- ((QueryImpl) q).getDelegate().setCandidateType(
- SimpleEntity.class, true);
+ OpenJPAQuery q = createMethodQuery("invalidReturnMeth");
try {
q.getResultList().get(0);
fail("should have gotten an exception since method is invalid");
@@ -64,10 +84,7 @@
}
public void testVoidMethodReturnType() {
- OpenJPAQuery q = em.createQuery(QueryLanguages.LANG_METHODQL,
- getClass().getName() + ".voidMeth");
- ((QueryImpl) q).getDelegate().setCandidateType(
- SimpleEntity.class, true);
+ OpenJPAQuery q = createMethodQuery("voidMeth");
try {
q.getResultList().get(0);
fail("should have gotten an exception since method is invalid");
@@ -75,10 +92,14 @@
// expected
}
}
-
+
+ /**
+ * Returns the list whose element is the Map of input parameters.
+ * @return
+ */
public static ResultObjectProvider echo(StoreContext ctx,
ClassMetaData meta, boolean subs, Map params, FetchConfiguration conf) {
- return new ListResultObjectProvider(new ArrayList(params.values()));
+ return new ListResultObjectProvider(Collections.singletonList(params));
}
public static void voidMeth(StoreContext ctx,
Modified: openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_runtime.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_runtime.xml?rev=750798&r1=750797&r2=750798&view=diff
==============================================================================
--- openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_runtime.xml (original)
+++ openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_runtime.xml Fri Mar 6 07:50:54 2009
@@ -1372,21 +1372,32 @@
...
// the method query language is 'openjpa.MethodQL'.
-// set the query string to the method to execute, including full class name; if
-// the class is in the candidate class' package or in the query imports, you
-// can omit the package; if the method is in the candidate class, you can omit
-// the class name and just specify the method name
-OpenJPAEntityManager oem = OpenJPAPersistence.cast(emf);
-OpenJPAQuery q = oem.createQuery("openjpa.MethodQL", "com.xyz.Finder.getByName");
+// set the query string to the target method to execute, prefixed by fullly-
+// qualified class name.
+// If a candidate class has been specified for the query, then if the class is
+// in the candidate class' package or in the query imports, you can omit the
+// package. If the method is in the candidate class, you can omit the class name
+// and just specify the method name.
-// set the type of objects that the method returns
-q.setResultClass(Person.class);
+ OpenJPAEntityManager oem = OpenJPAPersistence.cast(emf);
+ OpenJPAQuery q = oem.createQuery("openjpa.MethodQL", "com.xyz.Finder.getByName");
-// parameters are passed the same way as in standard queries
-q.setParameter("firstName", "Fred").setParameter("lastName", "Lucas");
+// parameters are passed the same way as in standard queries
+// but you have to declare the parameters with their types on the implementation
+
+// Unwrap the implementation and declare parameters with types in a
+// comma-separated list
+ q.unwrap(org.apache.openjpa.kernel.Query.class)
+ .declareParameters("String firstName, String lastName");
-// this executes your method to get the results
-List results = q.getResultList();
+ q.setParameter("firstName", "Fred").setParameter("lastName", "Lucas");
+
+// this executes the target method to get the results
+ List results = q.getResultList();
+
+// The result is returned as a list but the element(s) in the list is determined
+// by the returned value of the target method
+
</programlisting>
<para>
For datastore queries, the method must have the following signature: