Author: mikedd
Date: Tue Feb 22 02:06:11 2011
New Revision: 1073201
URL: http://svn.apache.org/viewvc?rev=1073201&view=rev
Log:
OPENJPA-1944: Version defaults to 1 if the column returns null from the database
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/IntVersion.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/ShortVersion.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/TimestampVersion.java
(with props)
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ColumnVersionStrategy.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=1073201&r1=1073200&r2=1073201&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 Feb 22 02:06:11 2011
@@ -21,6 +21,7 @@ package org.apache.openjpa.jdbc.meta.str
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.sql.SQLException;
+import java.sql.Timestamp;
import java.util.Comparator;
import org.apache.openjpa.jdbc.identifier.DBIdentifier;
@@ -263,15 +264,26 @@ public abstract class ColumnVersionStrat
// typically if one version column is in the result, they all are, so
// optimize by checking for the first one before doing any real work
Column[] cols = vers.getColumns();
- if (!res.contains(cols[0]))
+ if (!res.contains(cols[0])) {
return null;
+ }
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)
+ if (sm != null) {
sm.setVersion(version);
+ }
return version;
}
Added:
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=1073201&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java
Tue Feb 22 02:06:11 2011
@@ -0,0 +1,117 @@
+/*
+ * 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.version;
+
+import java.sql.Timestamp;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+
+import org.apache.openjpa.persistence.jdbc.version.model.IntVersion;
+import org.apache.openjpa.persistence.jdbc.version.model.ShortVersion;
+import org.apache.openjpa.persistence.jdbc.version.model.TimestampVersion;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+public class TestVersionColumn extends SQLListenerTestCase {
+ public void setUp() {
+ super.setUp(IntVersion.class, TimestampVersion.class,
ShortVersion.class);
+ }
+
+ public void testNullIntegerVersion() {
+ EntityManager em = emf.createEntityManager();
+ EntityTransaction tran = em.getTransaction();
+ tran.begin();
+ em.persist(new IntVersion());
+ em.persist(new IntVersion());
+ tran.commit();
+
+ tran.begin();
+ em.createNativeQuery("UPDATE IntVersion set version =
NULL").executeUpdate();
+ tran.commit();
+ em.clear();
+
+ resetSQL();
+
+ List<IntVersion> results = em.createQuery("SELECT i from IntVersion
i", IntVersion.class).getResultList();
+ 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("Unexpected number of SQL statements: " + getSQLCount(),
1, getSQLCount());
+
+ em.close();
+ }
+
+ public void testNullTimestampVersion() {
+ EntityManager em = emf.createEntityManager();
+ EntityTransaction tran = em.getTransaction();
+ tran.begin();
+ em.persist(new TimestampVersion());
+ em.persist(new TimestampVersion());
+ tran.commit();
+
+ tran.begin();
+ em.createNativeQuery("UPDATE TimestampVersion set version =
NULL").executeUpdate();
+ tran.commit();
+ em.clear();
+
+ resetSQL();
+ List<TimestampVersion> results =
+ em.createQuery("SELECT i from TimestampVersion i",
TimestampVersion.class).getResultList();
+ 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("Unexpected number of SQL statements: " + getSQLCount(),
1, getSQLCount());
+
+ em.close();
+ }
+
+ public void testNullShortVersion() {
+ EntityManager em = emf.createEntityManager();
+ EntityTransaction tran = em.getTransaction();
+ tran.begin();
+ em.persist(new ShortVersion());
+ em.persist(new ShortVersion());
+ tran.commit();
+
+ tran.begin();
+ em.createNativeQuery("UPDATE ShortVersion set version =
NULL").executeUpdate();
+ tran.commit();
+ em.clear();
+
+ resetSQL();
+ List<ShortVersion> results =
+ em.createQuery("SELECT i from ShortVersion i",
ShortVersion.class).getResultList();
+ 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("Unexpected number of SQL statements: " + getSQLCount(),
1, getSQLCount());
+
+ em.close();
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/TestVersionColumn.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/IntVersion.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/IntVersion.java?rev=1073201&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/IntVersion.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/IntVersion.java
Tue Feb 22 02:06:11 2011
@@ -0,0 +1,53 @@
+/*
+ * 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.version.model;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Version;
+
+@Entity
+@Table(name="IntVersion")
+public class IntVersion {
+ @Id
+ @GeneratedValue
+ private int id;
+
+ @Version
+ private int version;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public int getVersion() {
+ return version;
+ }
+
+ public void setVersion(int version) {
+ this.version = version;
+ }
+
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/IntVersion.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/ShortVersion.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/ShortVersion.java?rev=1073201&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/ShortVersion.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/ShortVersion.java
Tue Feb 22 02:06:11 2011
@@ -0,0 +1,53 @@
+/*
+ * 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.version.model;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Version;
+
+@Entity
+@Table(name="ShortVersion")
+public class ShortVersion {
+ @Id
+ @GeneratedValue
+ private int id;
+
+ @Version
+ private short version;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public short getVersion() {
+ return version;
+ }
+
+ public void setVersion(short version) {
+ this.version = version;
+ }
+
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/ShortVersion.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/TimestampVersion.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/TimestampVersion.java?rev=1073201&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/TimestampVersion.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/TimestampVersion.java
Tue Feb 22 02:06:11 2011
@@ -0,0 +1,55 @@
+/*
+ * 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.version.model;
+
+import java.sql.Timestamp;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Version;
+
+@Entity
+@Table(name = "TimestampVersion")
+public class TimestampVersion {
+ @Id
+ @GeneratedValue
+ private int id;
+
+ @Version
+ private Timestamp version;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public Timestamp getVersion() {
+ return version;
+ }
+
+ public void setVersion(Timestamp version) {
+ this.version = version;
+ }
+
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/version/model/TimestampVersion.java
------------------------------------------------------------------------------
svn:eol-style = native