You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by ht...@apache.org on 2013/10/18 01:00:39 UTC

svn commit: r1533280 - in /openjpa/branches/2.1.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/

Author: hthomann
Date: Thu Oct 17 23:00:39 2013
New Revision: 1533280

URL: http://svn.apache.org/r1533280
Log:
OPENJPA-2435: Version field in a projection always returned as an Integer.

Added:
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/BaseEntity.java
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ChildVersionEntity.java
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/LongVersionEntity.java
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeLongVersionEntity.java
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeShortVersionEntity.java
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ShortVersionEntity.java
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TestVersionFieldType.java
    openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TimestampVersionEntity.java
Modified:
    openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java

Modified: openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java?rev=1533280&r1=1533279&r2=1533280&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java (original)
+++ openjpa/branches/2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java Thu Oct 17 23:00:39 2013
@@ -18,12 +18,12 @@
  */
 package org.apache.openjpa.jdbc.meta.strats;
 
-import java.util.Map;
 import java.util.HashMap;
+import java.util.Map;
 
-import org.apache.openjpa.meta.JavaTypes;
-import org.apache.openjpa.util.InternalException;
+import org.apache.openjpa.jdbc.meta.FieldMapping;
 import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.meta.JavaTypes;
 
 /**
  * Uses a version number for optimistic versioning.
@@ -36,7 +36,8 @@ public class NumberVersionStrategy
     public static final String ALIAS = "version-number";
 
     private Number _initial = 1;
-
+    private Integer _javaType = null;
+    
     /**
      * Set the initial value for version column. Defaults to 1.
      */
@@ -56,7 +57,13 @@ public class NumberVersionStrategy
     }
 
     protected int getJavaType() {
-        return JavaTypes.INT;
+        if (_javaType == null && vers.getClassMapping().getVersionFieldMapping() != null) {
+            _javaType = Integer.valueOf(vers.getClassMapping().getVersionFieldMapping().getTypeCode());
+        } else {
+            return JavaTypes.INT;
+        }
+
+        return _javaType;
     }
     
     protected Object nextVersion(Object version) {

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/BaseEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/BaseEntity.java?rev=1533280&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/BaseEntity.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/BaseEntity.java Thu Oct 17 23:00:39 2013
@@ -0,0 +1,30 @@
+/*
+ * 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.jpql.version.type;
+
+import javax.persistence.*;
+
+@MappedSuperclass
+abstract class BaseEntity {
+        
+    @Version
+    protected Long version;
+
+    public Long getVersion() { return version; }
+}

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ChildVersionEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ChildVersionEntity.java?rev=1533280&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ChildVersionEntity.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ChildVersionEntity.java Thu Oct 17 23:00:39 2013
@@ -0,0 +1,33 @@
+/*
+ * 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.jpql.version.type;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class ChildVersionEntity extends BaseEntity {
+    
+    @Id
+    private int id;
+        
+    public int getId() { return id; }
+    public void setId(int id) { this.id = id; }
+}
+

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/LongVersionEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/LongVersionEntity.java?rev=1533280&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/LongVersionEntity.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/LongVersionEntity.java Thu Oct 17 23:00:39 2013
@@ -0,0 +1,38 @@
+/*
+ * 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.jpql.version.type;
+
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+import javax.persistence.*;
+
+@Entity
+public class LongVersionEntity implements Serializable {
+    @Id
+    private int id;
+    
+    @Version
+    protected Long version;
+    
+    public int getId() { return id; }
+    public void setId(int id) { this.id = id; }
+    public Long getVersion() { return version; }
+}
+

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeLongVersionEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeLongVersionEntity.java?rev=1533280&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeLongVersionEntity.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeLongVersionEntity.java Thu Oct 17 23:00:39 2013
@@ -0,0 +1,37 @@
+/*
+ * 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.jpql.version.type;
+
+import java.io.Serializable;
+
+import javax.persistence.*;
+
+@Entity
+public class PrimativeLongVersionEntity implements Serializable {
+    @Id
+    private int id;
+    
+    @Version
+    protected long version;
+
+    public int getId() { return id; }
+    public void setId(int id) { this.id = id; }
+    public long getVersion() { return version; }
+}
+

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeShortVersionEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeShortVersionEntity.java?rev=1533280&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeShortVersionEntity.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeShortVersionEntity.java Thu Oct 17 23:00:39 2013
@@ -0,0 +1,37 @@
+/*
+ * 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.jpql.version.type;
+
+import java.io.Serializable;
+
+import javax.persistence.*;
+
+@Entity
+public class PrimativeShortVersionEntity implements Serializable {
+    @Id
+    private int id;
+    
+    @Version
+    protected short version;
+
+    public int getId() { return id; }
+    public void setId(int id) { this.id = id; }
+    public short getVersion() { return version; }
+}
+

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ShortVersionEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ShortVersionEntity.java?rev=1533280&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ShortVersionEntity.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ShortVersionEntity.java Thu Oct 17 23:00:39 2013
@@ -0,0 +1,37 @@
+/*
+ * 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.jpql.version.type;
+
+import java.io.Serializable;
+
+import javax.persistence.*;
+
+@Entity
+public class ShortVersionEntity implements Serializable {
+    @Id
+    private int id;
+    
+    @Version
+    protected Short version;
+
+    public int getId() { return id; }
+    public void setId(int id) { this.id = id; }
+    public Short getVersion() { return version; }
+}
+

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TestVersionFieldType.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TestVersionFieldType.java?rev=1533280&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TestVersionFieldType.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TestVersionFieldType.java Thu Oct 17 23:00:39 2013
@@ -0,0 +1,100 @@
+/*
+ * 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.jpql.version.type;
+
+import java.sql.Timestamp;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Verifies that the version field is of the proper java type 
+ * when returned from a query. See OPENJPA-2435.
+ */
+public class TestVersionFieldType extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(CLEAR_TABLES, LongVersionEntity.class, 
+            ShortVersionEntity.class, PrimativeLongVersionEntity.class,
+            PrimativeShortVersionEntity.class, TimestampVersionEntity.class, 
+            BaseEntity.class, ChildVersionEntity.class);
+        createTestData();
+    }
+
+    public void testProjectionVersionReturnType() {
+        verifyType(LongVersionEntity.class, Long.class);
+        verifyType(ShortVersionEntity.class, Short.class);
+        verifyType(PrimativeShortVersionEntity.class, Short.class);
+        verifyType(PrimativeLongVersionEntity.class, Long.class);
+        verifyType(ChildVersionEntity.class, Long.class);
+        verifyType(TimestampVersionEntity.class, Timestamp.class);
+    }
+
+    public void verifyType(Class<?> cls, Class<?> expectedClsType) {
+
+        EntityManager em = emf.createEntityManager();
+        String str = "SELECT o.id, o.version FROM " + cls.getName() + " o";
+        Query query = em.createQuery(str);
+        List<Object[]> objectList = query.getResultList();
+
+        for (Object[] objects : objectList) {
+            assertNotNull("Version should not be null.", objects[1]);
+            assertTrue("Type should be " + expectedClsType.getName() + 
+                ".  But it is " + objects[1].getClass(),
+                objects[1].getClass() == expectedClsType);
+        }
+        
+        em.close();
+    }
+
+    public void createTestData() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        LongVersionEntity lve = new LongVersionEntity();
+        lve.setId(9);
+        em.persist(lve);
+
+        ShortVersionEntity sve = new ShortVersionEntity();
+        sve.setId(9);
+        em.persist(sve);
+
+        PrimativeShortVersionEntity psve = new PrimativeShortVersionEntity();
+        psve.setId(9);
+        em.persist(psve);
+
+        PrimativeLongVersionEntity plve = new PrimativeLongVersionEntity();
+        plve.setId(9);
+        em.persist(plve);
+
+        TimestampVersionEntity tve = new TimestampVersionEntity();
+        tve.setId(9);
+        em.persist(tve);
+
+        ChildVersionEntity ave = new ChildVersionEntity();
+        ave.setId(9);
+        em.persist(ave);
+
+        em.getTransaction().commit();
+        em.close();
+    }
+}

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TimestampVersionEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TimestampVersionEntity.java?rev=1533280&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TimestampVersionEntity.java (added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TimestampVersionEntity.java Thu Oct 17 23:00:39 2013
@@ -0,0 +1,38 @@
+/*
+ * 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.jpql.version.type;
+
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+import javax.persistence.*;
+
+@Entity
+public class TimestampVersionEntity implements Serializable {
+    @Id
+    private int id;
+
+    @Version
+    protected Timestamp version;
+
+    public int getId() { return id; }
+    public void setId(int id) { this.id = id; }
+    public Timestamp getVersion() { return version; }
+}
+