openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r745891 - in /openjpa/branches/1.2.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/
Date Thu, 19 Feb 2009 15:02:58 GMT
Author: mikedd
Date: Thu Feb 19 15:02:57 2009
New Revision: 745891

URL: http://svn.apache.org/viewvc?rev=745891&view=rev
Log:
OPENJPA-925 committing patch provided by Donald Woods. Bidirectional OneToOne relation incorrectly
set in loadEagerJoin fix for trunk written by Catalina Wei

Added:
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneChild.java
  (with props)
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneParent.java
  (with props)
Modified:
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java?rev=745891&r1=745890&r2=745891&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
Thu Feb 19 15:02:57 2009
@@ -524,19 +524,28 @@
         PersistenceCapable mappedByValue = null;
 
         if (mappedByFieldMapping != null) {
-        	ValueMapping val = mappedByFieldMapping.getValueMapping();
-        	ClassMetaData decMeta = val.getTypeMetaData();
-        	// this inverse field does not have corresponding classMapping
-        	// its value may be a collection/map etc.
-        	if (decMeta != null) {
-        	    mappedByValue = sm.getPersistenceCapable();
-        	    res.setMappedByFieldMapping(mappedByFieldMapping);
-        	    res.setMappedByValue(mappedByValue);
-        	}
+            ValueMapping val = mappedByFieldMapping.getValueMapping();
+            ClassMetaData decMeta = val.getTypeMetaData();
+            // eager loading a child from its toOne parent and
+            // the parent has @OneToOne(mappedBy="parent") child relation.
+            // By saving the mapped-by info in 'res' is to
+            // avoid unneeded SQL pushdown that would otherwise gets
+            // generated.
+            if (decMeta != null) {
+                mappedByValue = sm.getPersistenceCapable();
+                res.setMappedByFieldMapping(mappedByFieldMapping);
+                res.setMappedByValue(mappedByValue);
+            }
         }
 
         sm.storeObject(field.getIndex(), res.load(cls, store, fetch,
-            eagerJoin(res.newJoins(), cls, false)));
+        eagerJoin(res.newJoins(), cls, false)));
+
+        // reset mapped by is needed for OneToOne bidirectional relations
+        // having a mapped-by parent to correctly set the parent-child
+        // relation.
+        res.setMappedByFieldMapping(null);
+        res.setMappedByValue(null);
     }
 
     public void load(OpenJPAStateManager sm, JDBCStore store,

Added: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneChild.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneChild.java?rev=745891&view=auto
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneChild.java
(added)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneChild.java
Thu Feb 19 15:02:57 2009
@@ -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.relations;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+import javax.persistence.Version;
+
+@Entity
+public class OneOneChild {
+
+    @Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @OneToOne
+    private OneOneParent parent;
+
+    @Version
+    private Integer optLock;
+
+    public long getId() { 
+        return id; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+
+    public OneOneParent getParent() { 
+        return parent; 
+    }
+
+    public void setParent(OneOneParent parent) { 
+        this.parent = parent; 
+    }
+}

Propchange: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneChild.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneParent.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneParent.java?rev=745891&view=auto
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneParent.java
(added)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneParent.java
Thu Feb 19 15:02:57 2009
@@ -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.relations;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+import javax.persistence.Version;
+
+@Entity
+public class OneOneParent {
+
+    @Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @OneToOne(mappedBy="parent")
+    private OneOneChild child;
+
+    @Version
+    private Integer optLock;
+
+    public long getId() { 
+        return id; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+
+    public OneOneChild getChild() { 
+        return child; 
+    }
+
+    public void setChild(OneOneChild child) { 
+        this.child = child; 
+    }
+}

Propchange: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/OneOneParent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java?rev=745891&r1=745890&r2=745891&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java
(original)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestManyEagerSQL.java
Thu Feb 19 15:02:57 2009
@@ -33,10 +33,11 @@
     extends SQLListenerTestCase {
 
     public void setUp() {
-        setUp(
+        setUp(DROP_TABLES,
             OneManyEagerParent.class, OneManyEagerChild.class,
-            OneManyLazyChild.class);
-        
+            OneManyLazyChild.class, OneOneParent.class, 
+            OneOneChild.class);
+
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
 
@@ -56,6 +57,17 @@
             em.persist(parent);
         }
 
+        for (int i = 0; i < 3; i++) {
+            OneOneParent parent = new OneOneParent();
+            parent.setName("parent" + i);
+            OneOneChild child = new OneOneChild();
+            child.setName("child" + i);
+            parent.setChild(child);
+            child.setParent(parent);
+            em.persist(child);
+            em.persist(parent);
+        }
+
         em.flush();
         em.getTransaction().commit();
         em.close();
@@ -190,6 +202,60 @@
         em.close();
     }
 
+    public void testOneToOneParentQuery() {
+        sql.clear();
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        String query = "select p FROM OneOneParent p";
+        Query q = em.createQuery(query);
+        List list = q.getResultList();
+        assertEquals(3, list.size());
+
+        // Expected SQLs:
+        //   SELECT t0.id, t0.optLock, t1.id, t1.optLock, t1.name, t0.name
+        //   FROM OneOneParent t0 
+        //   LEFT OUTER JOIN OneOneChild t1 ON t0.id = t1.PARENT_ID
+
+        assertEquals(1, sql.size());
+
+        sql.clear();
+
+        for (int i = 0; i < list.size(); i++) {
+            OneOneParent p = (OneOneParent) list.get(i);
+            assertEquals(p, p.getChild().getParent());
+        }
+
+        assertEquals(0, sql.size());
+        em.close();
+    }
+
+    public void testOneToOneChildQuery() {
+        sql.clear();
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        String query = "select c FROM OneOneChild c";
+        Query q = em.createQuery(query);
+        List list = q.getResultList();
+        assertEquals(3, list.size());
+
+        // Expected SQLs:
+        //   SELECT t0.id, t0.optLock, t1.id, t1.optLock, t1.name, t0.name 
+        //   FROM OneOneParent t0 
+        //   LEFT OUTER JOIN OneOneChild t1 ON t0.id = t1.PARENT_ID
+
+        assertEquals(1, sql.size());
+
+        sql.clear();
+
+        for (int i = 0; i < list.size(); i++) {
+            OneOneChild c = (OneOneChild) list.get(i);
+            assertEquals(c, c.getParent().getChild());
+        }
+
+        assertEquals(0, sql.size());
+        em.close();
+    }
+
     public static void main(String[] args) {
         TestRunner.run(TestManyEagerSQL.class);
     }



Mime
View raw message