You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2012/08/02 14:54:26 UTC

svn commit: r1368438 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/d...

Author: curtisr7
Date: Thu Aug  2 12:54:26 2012
New Revision: 1368438

URL: http://svn.apache.org/viewvc?rev=1368438&view=rev
Log:
OPENJPA-2245: Fix NotSerializableException when using Criteria and QueryCache.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestRemoteQueryCacheCriteriaQuery.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/SerializingConcurrentQueryCache.java
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryKey.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryKey.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryKey.java?rev=1368438&r1=1368437&r2=1368438&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryKey.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/QueryKey.java Thu Aug  2 12:54:26 2012
@@ -245,9 +245,9 @@ public class QueryKey
         key._accessPathClassNames = accessPathClassNames;
         key._timeout = timeout;
         key._query = q.getQueryString();
-        if (key._query == null) {
-            // this can be a criteria query
-            key._query = parsed;
+        if (key._query == null && parsed != null) {
+            // this is a criteria query. Store the Stringified query value rather than the full cq.
+            key._query = parsed.toString();
         }
         key._ignoreChanges = q.getIgnoreChanges();
         key._rangeStart = startIdx;

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestRemoteQueryCacheCriteriaQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestRemoteQueryCacheCriteriaQuery.java?rev=1368438&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestRemoteQueryCacheCriteriaQuery.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestRemoteQueryCacheCriteriaQuery.java Thu Aug  2 12:54:26 2012
@@ -0,0 +1,61 @@
+/*
+ * 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.criteria;
+
+import javax.persistence.EntityManager;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
+import javax.persistence.metamodel.EntityType;
+import javax.persistence.metamodel.Metamodel;
+
+import org.apache.openjpa.persistence.datacache.SerializingConcurrentQueryCache;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestRemoteQueryCacheCriteriaQuery extends SingleEMFTestCase {
+    protected EntityType<Department> department_ = null;
+    protected OpenJPACriteriaBuilder cb;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp("openjpa.DataCache", "true", "openjpa.QueryCache",
+            SerializingConcurrentQueryCache.SERIALIZING_CONCURRENT_QUERY_CACHE, Department.class, Employee.class,
+            Contact.class, Manager.class, FrequentFlierPlan.class);
+        EntityManager em = emf.createEntityManager();
+        try {
+            Metamodel mm = em.getMetamodel();
+            department_ = mm.entity(Department.class);
+            cb = emf.getCriteriaBuilder();
+        } finally {
+            em.close();
+        }
+    }
+
+    public void test() {
+        CriteriaQuery<Department> q = cb.createQuery(Department.class);
+        Root<Department> dept = q.from(Department.class);
+        q.select(dept).where(cb.equal(dept.get(department_.getSingularAttribute("name", String.class)), "test"));
+
+        EntityManager em = emf.createEntityManager();
+        try {
+            em.createQuery(q).getResultList();
+        } finally {
+            em.close();
+        }
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/SerializingConcurrentQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/SerializingConcurrentQueryCache.java?rev=1368438&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/SerializingConcurrentQueryCache.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/SerializingConcurrentQueryCache.java Thu Aug  2 12:54:26 2012
@@ -0,0 +1,63 @@
+/*
+ * 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.datacache;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import org.apache.openjpa.datacache.ConcurrentQueryCache;
+import org.apache.openjpa.datacache.QueryKey;
+import org.apache.openjpa.datacache.QueryResult;
+
+/**
+ * A QueryCache implementation that serializes all keys / values to simulate having a remote cache.
+ */
+public class SerializingConcurrentQueryCache extends ConcurrentQueryCache {
+    private static final long serialVersionUID = 1L;
+    public static String SERIALIZING_CONCURRENT_QUERY_CACHE = SerializingConcurrentQueryCache.class.getName();
+
+    @Override
+    public QueryResult get(QueryKey key) {
+        return roundtrip(super.get(roundtrip(key)));
+    }
+
+    @Override
+    public QueryResult put(QueryKey qk, QueryResult oids) {
+        roundtrip(qk);
+        roundtrip(oids);
+        return roundtrip(super.put(qk, oids));
+    }
+
+    @SuppressWarnings("unchecked")
+    private static <T> T roundtrip(T o) {
+        try {
+            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+            ObjectOutputStream out = new ObjectOutputStream(bytes);
+            out.writeObject(o);
+            out.flush();
+            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
+            return (T) in.readObject();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+}