You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2010/05/12 21:36:23 UTC

svn commit: r943647 - in /openjpa/branches/1.0.x: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persist...

Author: mikedd
Date: Wed May 12 19:36:22 2010
New Revision: 943647

URL: http://svn.apache.org/viewvc?rev=943647&view=rev
Log:
OPENJPA-589: Can not retrieve M-to-M data when DataCache is on.
Submitted By: Heath Thomann, merged from Fay's changes in trunk and 1.1.x

Added:
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java   (with props)
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityE.java   (with props)
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityF.java   (with props)
Modified:
    openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractPCData.java

Modified: openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractPCData.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractPCData.java?rev=943647&r1=943646&r2=943647&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractPCData.java (original)
+++ openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractPCData.java Wed May 12 19:36:22 2010
@@ -86,18 +86,22 @@ public abstract class AbstractPCData
             case JavaTypes.MAP:
                 Map m = (Map) data;
                 Map m2 = (Map) sm.newFieldProxy(fmd.getIndex());
-                Collection keys = new ArrayList (m.size());
+                Collection keys = new ArrayList(m.size());
+                Collection values = new ArrayList(m.size());
+                Map.Entry e;
 
-                for (Iterator mi = m.entrySet().iterator(); mi.hasNext();)
-                    keys.add(mi.next());
+                Iterator itr = m.entrySet().iterator();
+                while (itr.hasNext()) {
+                    e = (Map.Entry) itr.next();
+                    keys.add(e.getKey());
+                    values.add(e.getValue());
+                }
 
                 Object[] keyArray = keys.toArray();
-                Object[] values = toNestedFields(sm, fmd.getElement(),
-                    keys, fetch, context).toArray();
-                int idx = 0;
-                for (Iterator mi = m.entrySet().iterator(); mi.hasNext(); idx++)
-                    m2.put(keyArray[idx], values[idx]);
-
+                Object[] valueArray = toNestedFields(sm, fmd.getElement(), values, fetch, context).toArray();
+                for (int idx = 0; idx < keyArray.length; idx++) {
+                    m2.put(keyArray[idx], valueArray[idx]);
+                }
                 return m2;
             case JavaTypes.ARRAY:
                 int length = Array.getLength(data);

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java?rev=943647&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java (added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java Wed May 12 19:36:22 2010
@@ -0,0 +1,77 @@
+/*
+ * 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.util.Map;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.datacache.common.apps.M2MEntityE;
+import org.apache.openjpa.persistence.datacache.common.apps.M2MEntityF;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestM2MInDataCache extends SingleEMFTestCase {
+    public void setUp() {
+        setUp("openjpa.DataCache", "true", "openjpa.RemoteCommitProvider", "sjvm", M2MEntityE.class, M2MEntityF.class,
+            CLEAR_TABLES);
+    }
+
+    /**
+     * Test if child list is in order after new child list is added in setup().
+     */
+    public void testM2MDataCache() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        M2MEntityE e1 = new M2MEntityE();
+        e1.setId(1);
+        e1.setName("ABC");
+        em.persist(e1);
+        M2MEntityE e2 = new M2MEntityE();
+        e2.setId(2);
+        e2.setName("DEF");
+        em.persist(e2);
+
+        M2MEntityF f1 = new M2MEntityF();
+        f1.setId(10);
+        em.persist(f1);
+        M2MEntityF f2 = new M2MEntityF();
+        f2.setId(20);
+        em.persist(f2);
+
+        e1.getEntityF().put(f1.getId(), f1);
+        e1.getEntityF().put(f2.getId(), f2);
+        e2.getEntityF().put(f1.getId(), f1);
+        e2.getEntityF().put(f2.getId(), f2);
+
+        f1.getEntityE().put(e1.getName(), e1);
+        f1.getEntityE().put(e2.getName(), e2);
+        f2.getEntityE().put(e1.getName(), e1);
+        f2.getEntityE().put(e2.getName(), e2);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        // check that we can find the entities and retrieve their map fields
+        assertNotNull(em.find(M2MEntityE.class, 1).getEntityF());
+        assertNotNull(em.find(M2MEntityE.class, 2).getEntityF());
+        assertNotNull(em.find(M2MEntityF.class, 10).getEntityE());
+        assertNotNull(em.find(M2MEntityF.class, 20).getEntityE());
+    }
+}

Propchange: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestM2MInDataCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityE.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityE.java?rev=943647&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityE.java (added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityE.java Wed May 12 19:36:22 2010
@@ -0,0 +1,67 @@
+/*
+ * 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.common.apps;
+
+import javax.persistence.*;
+import java.util.*;
+
+@Entity
+public class M2MEntityE {
+    @Id
+    private int id;
+
+    private String name;
+
+    @ManyToMany
+    @MapKey(name = "id")
+    private Map<Integer, M2MEntityF> entityf;
+
+    public M2MEntityE() {
+        entityf = new HashMap<Integer, M2MEntityF>();
+        name = "entitye";
+    }
+
+    public Map<Integer, M2MEntityF> getEntityF() {
+        return entityf;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String toString() {
+        return "EntityE:" + id;
+    }
+
+    public void print() {
+        System.out.println("EntityD id=" + id + " entityc=" + entityf);
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

Propchange: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityE.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityF.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityF.java?rev=943647&view=auto
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityF.java (added)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityF.java Wed May 12 19:36:22 2010
@@ -0,0 +1,56 @@
+/*
+ * 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.common.apps;
+
+import javax.persistence.*;
+import java.util.*;
+
+@Entity
+public class M2MEntityF {
+    @Id
+    private int id;
+
+    @ManyToMany(mappedBy = "entityf")
+    @MapKey(name = "name")
+    private Map<String, M2MEntityE> entitye;
+
+    public M2MEntityF() {
+        entitye = new HashMap<String, M2MEntityE>();
+    }
+
+    public Map<String, M2MEntityE> getEntityE() {
+        return entitye;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String toString() {
+        return "EntityF:" + id;
+    }
+
+    public void print() {
+        System.out.println("EntityF id=" + id + " entitye=" + entitye);
+    }
+}

Propchange: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/common/apps/M2MEntityF.java
------------------------------------------------------------------------------
    svn:eol-style = native