openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r1084265 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/ op...
Date Tue, 22 Mar 2011 17:41:59 GMT
Author: curtisr7
Date: Tue Mar 22 17:41:58 2011
New Revision: 1084265

URL: http://svn.apache.org/viewvc?rev=1084265&view=rev
Log:
OPENJPA-1944: Properly handle a null version field.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ColumnVersionStrategy.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/AnnoTest1.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ColumnVersionStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ColumnVersionStrategy.java?rev=1084265&r1=1084264&r2=1084265&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ColumnVersionStrategy.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ColumnVersionStrategy.java
Tue Mar 22 17:41:58 2011
@@ -270,15 +270,6 @@ public abstract class ColumnVersionStrat
 
         Object version = populateFromResult(res);
         
-        // we know the version column was part of the result - safe to initialize to 1.
-        if(version == null) { 
-            if (sm.getMetaData().getVersionField().getDeclaredTypeCode() == JavaTypes.DATE)
{
-                version = new Timestamp(1);
-            } else {
-                version = new Long(1);
-            }
-        }
-
         // OPENJPA-662 Allow a null StateManager because this method may just be
         // invoked to get the result of projection query
         if (sm != null) {

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java?rev=1084265&r1=1084264&r2=1084265&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
Tue Mar 22 17:41:58 2011
@@ -3069,7 +3069,10 @@ public class StateManagerImpl
             // always be set after the first state load or set (which is why
             // we do this even if no fields were loaded -- could be that this
             // method is being called after a field is set)
-            if (_loadVersion == null && (_meta == null || _meta.getVersionField()
!= null)) {
+            // If the _loadVersion field is null AND the version field has been loaded, skip
calling sync version.
+            // This indicates that the DB has a null value for the version column. 
+            FieldMetaData versionMeta = _meta != null ? _meta.getVersionField() : null;
+            if (_loadVersion == null && (versionMeta != null && !_loaded.get(versionMeta.getIndex())))
{
                 syncVersion(sdata);
                 ret = ret || _loadVersion != null;
             }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/AnnoTest1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/AnnoTest1.java?rev=1084265&r1=1084264&r2=1084265&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/AnnoTest1.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/AnnoTest1.java
Tue Mar 22 17:41:58 2011
@@ -41,7 +41,7 @@ public class AnnoTest1 {
 
     @Version
     @Column(name = "ANNOVER")
-    protected int version;
+    protected Integer version;
 
     @Basic
     protected int basic;

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java?rev=1084265&r1=1084264&r2=1084265&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java
Tue Mar 22 17:41:58 2011
@@ -34,7 +34,10 @@ public class TestVersion extends SingleE
 
     public void setUp() {
         setUp(AnnoTest1.class, AnnoTest2.class, AnnoTest3.class, Flat1.class,
-            EmbedOwner.class, EmbedValue.class, CLEAR_TABLES);
+            EmbedOwner.class, EmbedValue.class, CLEAR_TABLES 
+            ,"openjpa.Log","SQL=trace"
+            ,"openjpa.ConnectionFactoryProperties","printParameters=true"
+        );
 
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
@@ -192,4 +195,37 @@ public class TestVersion extends SingleE
             cls.getVersion().getStrategy());
         assertEquals(1, cls.getVersion().getColumns().length);
     }
+
+    public void testNullInitialVersion() {
+        EntityManager em = emf.createEntityManager();
+        EntityManager em2 = emf.createEntityManager();
+        try {
+            AnnoTest1 e = new AnnoTest1(System.currentTimeMillis());
+            em.getTransaction().begin();
+            em.persist(e);
+            em.createQuery("UPDATE AnnoTest1 a SET a.version=null where a.pk=:pk").setParameter("pk",
e.getPk())
+                .executeUpdate();
+            em.getTransaction().commit();
+            em.close();
+             em = emf.createEntityManager();
+            
+            em.getTransaction().begin();
+            em2.getTransaction().begin();
+            
+            AnnoTest1 e2 = em2.find(AnnoTest1.class, e.getPk());
+            e = em.find(AnnoTest1.class, e.getPk());
+            e.setBasic(1);
+            em.getTransaction().commit();
+            
+            e2 = em2.find(AnnoTest1.class, e.getPk());
+            em2.refresh(e2);
+            System.out.println(e2.getBasic());
+        } finally {
+            if (em.getTransaction().isActive()) {
+                em.getTransaction().rollback();
+            }
+            em.close();
+        }
+    }
+
 }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java?rev=1084265&r1=1084264&r2=1084265&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java
Tue Mar 22 17:41:58 2011
@@ -53,7 +53,8 @@ public class TestVersionColumn extends S
         assertNotNull("No results found", results);
         assertFalse("No results found", results.isEmpty());
         for (IntVersion iv : results) {
-            assertEquals("Version should be initialized to 1, was: " + iv.getVersion(), 1,
iv.getVersion());
+            assertEquals("Version should be initialized to 0, was: " + iv.getVersion(), 0,
iv.getVersion());
+            em.find(IntVersion.class, iv.getId());
         }
 
         assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount());
@@ -80,7 +81,8 @@ public class TestVersionColumn extends S
         assertNotNull("No results found", results);
         assertFalse("No results found", results.isEmpty());
         for (TimestampVersion iv : results) {
-            assertEquals("Version should be initialized to 1" + iv.getVersion(), new Timestamp(1),
iv.getVersion());
+            assertEquals("Version should be initialized to null, was: " + iv.getVersion(),
null, iv.getVersion());
+            em.find(TimestampVersion.class, iv.getId());
         }
 
         assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount());
@@ -107,9 +109,11 @@ public class TestVersionColumn extends S
         assertNotNull("No results found", results);
         assertFalse("No results found", results.isEmpty());
         for (ShortVersion iv : results) {
-            assertEquals("Version should be initialized to 1" + iv.getVersion(), 1, iv.getVersion());
+            assertEquals("Version should be initialized to 0, was" + iv.getVersion(), 0,
iv.getVersion());
+            em.find(ShortVersion.class, iv.getId());
         }
 
+
         assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount());
 
         em.close();



Mime
View raw message