openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r633738 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-kernel/src/main/java/org/apache/openjpa/meta/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/
Date Wed, 05 Mar 2008 01:12:21 GMT
Author: pcl
Date: Tue Mar  4 17:12:19 2008
New Revision: 633738

URL: http://svn.apache.org/viewvc?rev=633738&view=rev
Log:
OPENJPA-521. Committing on behalf of Qin Feng.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java?rev=633738&r1=633737&r2=633738&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCachePCDataImpl.java
Tue Mar  4 17:12:19 2008
@@ -145,7 +145,7 @@
 
         // If this field is used in "order by", we need to invalidate cache
         // for the collection that refer to this field.
-        if (sm.getPCState() == PCState.PDIRTY) {
+        if ((sm.getPCState() == PCState.PDIRTY) && fmd.isUsedInOrderBy()) {
             clearInverseRelationCache(sm, fmd);
         }
     }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java?rev=633738&r1=633737&r2=633738&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/FieldMetaData.java
Tue Mar  4 17:12:19 2008
@@ -207,6 +207,8 @@
     // ordering on load
     private Order[] _orders = null;
     private String _orderDec = null;
+    // indicate if this field is used by other field as "order by" value 
+    private boolean _usedInOrderBy = false;
 
     /**
      * Constructor.
@@ -1119,6 +1121,12 @@
                         decs[i] = decs[i].substring(0, spc);
                     }
                     orders[i] = getRepository().newOrder(this, decs[i], asc);
+                    //set "isUsedInOrderBy" to the field
+                    ClassMetaData elemCls = getElement()
+                        .getDeclaredTypeMetaData();
+                    FieldMetaData fmd = elemCls.getDeclaredField(decs[i]);
+                    if (fmd != null)
+                    	fmd.setUsedInOrderBy(true);                    
                 }
                 _orders = orders;
             }
@@ -2020,6 +2028,24 @@
 		_val.copy (vmd);
 	}
 
+    /**
+     * Check if this field is used by other field as "order by" value.
+     *
+     * @since 1.1.0
+     */
+    public boolean isUsedInOrderBy() {
+    	return _usedInOrderBy;
+    }
+    
+    /**
+     * Whether this field is used by other field as "order by" value .
+     *
+     * @since 1.1.0
+     */
+    public void setUsedInOrderBy(boolean isUsed) {
+    	_usedInOrderBy = isUsed;
+    }
+    
     /**
      * Serializable wrapper around a {@link Method} or {@link Field}. For 
      * space considerations, this does not support {@link Constructor}s.

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java?rev=633738&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyChild.java
Tue Mar  4 17:12:19 2008
@@ -0,0 +1,41 @@
+package org.apache.openjpa.persistence.datacache;
+
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.Entity;
+
+@Entity
+public class OrderedOneManyChild {
+    @Id
+    private long id;
+
+    private String name;
+
+    @ManyToOne
+    private OrderedOneManyParent parent;
+    
+    public long getId() { 
+        return id; 
+    }
+
+    public void setId(long id) { 
+        this.id = id; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+
+    public OrderedOneManyParent getParent() { 
+        return parent; 
+    }
+
+    public void setParent(OrderedOneManyParent parent) { 
+        this.parent = parent; 
+    }
+    
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java?rev=633738&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/OrderedOneManyParent.java
Tue Mar  4 17:12:19 2008
@@ -0,0 +1,41 @@
+package org.apache.openjpa.persistence.datacache;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.OrderBy;
+
+@Entity
+public class OrderedOneManyParent {
+	@Id
+    @GeneratedValue
+    private long id;
+
+    private String name;
+
+    @OneToMany(mappedBy="parent")
+    @OrderBy("name ASC")
+    private List<OrderedOneManyChild> children = 
+        new ArrayList<OrderedOneManyChild>();
+
+    public long getId() { 
+        return id; 
+    }
+
+    public List<OrderedOneManyChild> getChildren() { 
+        return children; 
+    }
+
+    public String getName() { 
+        return name; 
+    }
+
+    public void setName(String name) { 
+        this.name = name; 
+    }
+}
+

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java?rev=633738&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestOrderbyInDataCache.java
Tue Mar  4 17:12:19 2008
@@ -0,0 +1,97 @@
+package org.apache.openjpa.persistence.datacache;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Defined ordering is only applied when the collection is loaded from the datastore.
+ * It is not maintained by Openjpa as you modify the collection in memory. 
+ * Openjpa invalid data cache in case the persistence operation may result cache in wrong
order.
+ * This test suite tests various cases for the above problem.
+ */
+public class TestOrderbyInDataCache extends SingleEMFTestCase {
+	private long pid;
+    public void setUp() {
+        setUp("openjpa.DataCache", "true", 
+            "openjpa.RemoteCommitProvider", "sjvm", 
+            OrderedOneManyParent.class,
+            OrderedOneManyChild.class, CLEAR_TABLES);
+
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        OrderedOneManyParent prt = new OrderedOneManyParent();
+        prt.setName("Maria");
+        em.persist(prt);
+        //insert child in reverse order
+        for (int i = 4; i > 1; i--) {
+        	OrderedOneManyChild child = new OrderedOneManyChild();
+            child.setId(i);
+            child.setName("child" + i);
+            child.setParent(prt);
+            prt.getChildren().add(child);
+            em.persist(child);
+        }        
+        em.getTransaction().commit();
+        pid = prt.getId();
+        em.close();
+    }
+
+    /**
+     * Test if child list is in order after new child list is added in setup().
+     *
+     */
+    public void testGetChildList(){
+    	EntityManager em = emf.createEntityManager();
+    	OrderedOneManyParent prt = em.find(OrderedOneManyParent.class, pid);
+        assertEquals(3, prt.getChildren().size());
+        //the order should be "child2", "child3", "child4"
+        for (int i = 1; i < 4; i++) {
+            assertEquals("child" + (i + 1), prt.getChildren().
+                get(i-1).getName());
+        }   
+        em.close();
+    }
+    
+    public void testInsertChild() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        OrderedOneManyParent prt = em.find(OrderedOneManyParent.class, pid);
+        OrderedOneManyChild child = new OrderedOneManyChild();
+        child.setId(1);
+        child.setName("child1");
+        child.setParent(prt);
+        prt.getChildren().add(child);
+        em.persist(child);
+        em.getTransaction().commit();
+        em.close();
+        
+        //obtain object in new persistence context
+        em = emf.createEntityManager();
+        prt = em.find(OrderedOneManyParent.class, pid);
+        assertEquals(4, prt.getChildren().size());
+        //the order should be "child1", "child2", "child3", "child4"
+        for (int i = 1; i < 5; i++) {
+            assertEquals("child" + i, prt.getChildren().
+                get(i-1).getName());
+        }          
+        em.close();
+    }
+    
+    public void testUpdateChildName() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        OrderedOneManyChild child = em.find(OrderedOneManyChild.class, 4);
+        child.setName("child1");
+        em.persist(child);
+        em.getTransaction().commit();
+        OrderedOneManyParent prt = em.find(OrderedOneManyParent.class, pid);
+        assertEquals(3, prt.getChildren().size());
+        //the order should be "child1", "child2", "child3"
+        for (int i = 1; i < 4; i++) {
+            assertEquals("child" + i, prt.getChildren().
+                get(i-1).getName());
+        }          
+        em.close();    	
+    }
+}



Mime
View raw message