Author: ppoddar
Date: Thu Mar 18 18:32:19 2010
New Revision: 924938

URL: http://svn.apache.org/viewvc?rev=924938&view=rev
Log:
OPENJPA-1583: Timestamp version field in bulk update

Added:
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/NumericVersionedEntity.java
   (with props)
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java
   (with props)
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TimestampedEntity.java
   (with props)
Modified:
    
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Version.java
    
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/VersionStrategy.java
    
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java
    
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java

Modified: 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Version.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Version.java?rev=924938&r1=924937&r2=924938&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Version.java
 (original)
+++ 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Version.java
 Thu Mar 18 18:32:19 2010
@@ -368,12 +368,12 @@ public class Version
     }
 
     /**
-     * @return a Map<Column,String> specifying how to update each version
+     * @return a Map<Column,Object> specifying how to update each version
      * column in this instance during a bulk update.
      *
      * @since 1.0.0
      */
-    public Map getBulkUpdateValues() {
+    public Map<Column,?> getBulkUpdateValues() {
         return _strategy.getBulkUpdateValues();
     }
 }

Modified: 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/VersionStrategy.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/VersionStrategy.java?rev=924938&r1=924937&r2=924938&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/VersionStrategy.java
 (original)
+++ 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/VersionStrategy.java
 Thu Mar 18 18:32:19 2010
@@ -22,6 +22,7 @@ import java.sql.SQLException;
 import java.util.Map;
 
 import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.schema.Column;
 import org.apache.openjpa.jdbc.sql.Result;
 import org.apache.openjpa.jdbc.sql.Select;
 import org.apache.openjpa.kernel.OpenJPAStateManager;
@@ -77,10 +78,10 @@ public interface VersionStrategy
     public int compareVersion(Object v1, Object v2);
 
     /**
-     * @return a Map<Column,String> specifying how to update each version
+     * @return a Map<Column,Object> specifying how to update each version
      * column during a bulk update.
      *
      * @since 1.0.0
      */
-    public Map getBulkUpdateValues();
+    public Map<Column,? extends Object> getBulkUpdateValues();
 }

Modified: 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java?rev=924938&r1=924937&r2=924938&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java
 (original)
+++ 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java
 Thu Mar 18 18:32:19 2010
@@ -65,9 +65,9 @@ public class NumberVersionStrategy
         return ((Number) version).intValue() + 1;
     }
 
-    public Map getBulkUpdateValues() {
+    public Map<Column,String> getBulkUpdateValues() {
         Column[] cols = vers.getColumns();
-        Map map = new HashMap(cols.length);
+        Map<Column,String> map = new HashMap<Column,String>(cols.length);
         for (int i = 0; i < cols.length; i++)
             map.put(cols[i], cols[i].getName() + " + 1");
         return map;

Modified: 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=924938&r1=924937&r2=924938&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
 (original)
+++ 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
 Thu Mar 18 18:32:19 2010
@@ -2134,14 +2134,18 @@ public class DBDictionary
             Path path = (Path) updateParams.keySet().iterator().next();
             FieldMapping fm = (FieldMapping) path.last();
             ClassMapping meta = fm.getDeclaringMapping();
-            Map updates = meta.getVersion().getBulkUpdateValues();
-            for (Iterator iter = updates.entrySet().iterator();
-                iter.hasNext(); ) {
-                Map.Entry e = (Map.Entry) iter.next();
+            Map<Column,?> updates = meta.getVersion().getBulkUpdateValues();
+            for (Map.Entry e : updates.entrySet()) {
                 Column col = (Column) e.getKey();
-                String val = (String) e.getValue();
-                sql.append(", ").append(toDBName(col.getIdentifier()))
-                    .append(" = ").append(val);
+                Object val = e.getValue();
+                sql.append(", 
").append(toDBName(col.getIdentifier())).append(" = ");
+                // Version update value for Numeric version is encoded in a 
String
+                // to make SQL such as version = version+1 while Time stamp 
version is parameterized
+                if (val instanceof String) {
+                    sql.append((String)val);
+                } else {
+                    sql.appendValue(val);
+                }
             }
         }
     }

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/NumericVersionedEntity.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/NumericVersionedEntity.java?rev=924938&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/NumericVersionedEntity.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/NumericVersionedEntity.java
 Thu Mar 18 18:32:19 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.jdbc.update;
+
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.Version;
+
+/**
+ * An entity using a Timestamp as Version field.
+ * 
+ * 
+ * @author Pinaki Poddar
+ *
+ */
+...@entity
+public class NumericVersionedEntity {
+       @Id
+       @GeneratedValue
+       private long id;
+       
+       private String name;
+       
+       @Version
+       private int version;
+       
+
+       public long getId() {
+               return id;
+       }
+
+       public String getName() {
+               return name;
+       }
+
+       public void setName(String name) {
+               this.name = name;
+       }
+       
+       public int getVersion() {
+           return version;
+       }
+
+}

Propchange: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/NumericVersionedEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java?rev=924938&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java
 Thu Mar 18 18:32:19 2010
@@ -0,0 +1,87 @@
+/*
+ * 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.jdbc.update;
+
+import java.sql.Timestamp;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests for update on entity that uses a Timestamp as version.
+ * 
+ * @see <A 
HREF="https://issues.apache.org/jira/browse/OPENJPA-1583";>OPENJPA-1583</A>
+ *     
+ * @author Pinaki Poddar
+ * 
+ */
+public class TestTimestampVersion extends SingleEMFTestCase {
+       public void setUp() {
+               super.setUp(CLEAR_TABLES, TimestampedEntity.class, 
NumericVersionedEntity.class);
+       }
+
+    public void testBulkUpdateOnTimestampedVersion() {
+        TimestampedEntity pc = new TimestampedEntity();
+        pc.setName("Original");
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(pc);
+        em.getTransaction().commit();
+        
+        em.getTransaction().begin();
+        Timestamp oldVersion = pc.getVersion();
+        String jpql = "UPDATE TimestampedEntity t SET t.name=:newname WHERE 
t.name=:oldname";
+        em.createQuery(jpql)
+          .setParameter("newname", "Updated")
+          .setParameter("oldname", "Original")
+          .executeUpdate();
+        em.getTransaction().commit();
+        
+        em.getTransaction().begin();
+        em.refresh(pc);
+        Timestamp newVersion = pc.getVersion();
+        assertTrue(newVersion.after(oldVersion));
+    }
+    
+    public void testBulkUpdateOnNumericVersion() {
+        NumericVersionedEntity pc = new NumericVersionedEntity();
+        pc.setName("Original");
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(pc);
+        em.getTransaction().commit();
+        
+        em.getTransaction().begin();
+        int oldVersion = pc.getVersion();
+        String jpql = "UPDATE NumericVersionedEntity t SET t.name=:newname 
WHERE t.name=:oldname";
+        em.createQuery(jpql)
+          .setParameter("newname", "Updated")
+          .setParameter("oldname", "Original")
+          .executeUpdate();
+        em.getTransaction().commit();
+        
+        em.getTransaction().begin();
+        em.refresh(pc);
+        int newVersion = pc.getVersion();
+        assertEquals(newVersion, oldVersion+1);
+    }
+
+       
+}

Propchange: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TimestampedEntity.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TimestampedEntity.java?rev=924938&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TimestampedEntity.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TimestampedEntity.java
 Thu Mar 18 18:32:19 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.jdbc.update;
+
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.Version;
+
+/**
+ * An entity using a Timestamp as Version field.
+ * 
+ * 
+ * @author Pinaki Poddar
+ *
+ */
+...@entity
+public class TimestampedEntity {
+       @Id
+       @GeneratedValue
+       private long id;
+       
+       private String name;
+       
+       @Version
+       private Timestamp version;
+       
+
+       public long getId() {
+               return id;
+       }
+
+       public String getName() {
+               return name;
+       }
+
+       public void setName(String name) {
+               this.name = name;
+       }
+       
+       public Timestamp getVersion() {
+           return version;
+       }
+
+}

Propchange: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TimestampedEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to