Author: hthomann
Date: Mon Mar 28 12:54:36 2011
New Revision: 1086217

URL: http://svn.apache.org/viewvc?rev=1086217&view=rev
Log:
OPENJPA-677: Backported Pinaki's fixes for single table inheritance 
strategy/superclass oid issue.

Added:
    
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
   (with props)
    
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
   (with props)
    
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
   (with props)
    
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
   (with props)
Modified:
    
openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
    
openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java

Modified: 
openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java?rev=1086217&r1=1086216&r2=1086217&view=diff
==============================================================================
--- 
openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
 (original)
+++ 
openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
 Mon Mar 28 12:54:36 2011
@@ -172,6 +172,7 @@ public class ClassMapping
         FieldMapping fm;
         Joinable join;
         int pkIdx;
+        boolean canReadDiscriminator = true;        
         boolean isNullPK = true;
         for (int i = 0; i < pks.length; i++) {
             // we know that all pk column join mappings use primary key fields,
@@ -180,7 +181,7 @@ public class ClassMapping
             join = assertJoinable(pks[i]);
             fm = getFieldMapping(join.getFieldIndex());
             pkIdx = fm.getPrimaryKeyIndex();
-
+            canReadDiscriminator &= isSelfReference(fk, join.getColumns()); 
             // could have already set value with previous multi-column joinable
             if (vals[pkIdx] == null) {
                 res.startDataRequest(fm);
@@ -197,8 +198,13 @@ public class ClassMapping
         // the oid data is loaded by the base type, but if discriminator data
         // is present, make sure to use it to construct the actual oid instance
         // so that we get the correct app id class, etc
+        
+        // Discriminator refers to the row but the vals[] may hold data that
+        // refer to another row. Then there is little point reading the disc
+        // value
+
         ClassMapping dcls = cls;
-        if (subs) {
+        if (subs && canReadDiscriminator) {
             res.startDataRequest(cls.getDiscriminator());
             try {
                 Class dtype = cls.getDiscriminator().getClass(store, cls, res);
@@ -217,6 +223,15 @@ public class ClassMapping
         }
         return oid;
     }
+    
+    boolean isSelfReference(ForeignKey fk, Column[] cols) {
+       if (fk == null)
+               return true;
+       for (Column col : cols)
+               if (fk.getColumn(col) != col)
+                       return false;
+       return true;
+    }
 
     /**
      * Return the given column value(s) for the given object. The given

Modified: 
openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java?rev=1086217&r1=1086216&r2=1086217&view=diff
==============================================================================
--- 
openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
 (original)
+++ 
openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
 Mon Mar 28 12:54:36 2011
@@ -566,7 +566,7 @@ public class RelationFieldStrategy
         Object oid = null;
         if (relMapping.isMapped()) {
             oid = relMapping.getObjectId(store, res, field.getForeignKey(),
-                field.getPolymorphic() != ValueMapping.POLY_FALSE, null);
+                       field.getPolymorphic() != ValueMapping.POLY_FALSE, 
null);
         } else {
             Column[] cols = field.getColumns();
             if (relMapping.getIdentityType() == ClassMapping.ID_DATASTORE) {

Added: 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java?rev=1086217&view=auto
==============================================================================
--- 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
 (added)
+++ 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
 Mon Mar 28 12:54:36 2011
@@ -0,0 +1,86 @@
+/*
+ * 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.inheritance;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.inheritance.entity.Admin;
+import org.apache.openjpa.persistence.inheritance.entity.ComputerUser;
+import org.apache.openjpa.persistence.inheritance.entity.RegularUser;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests entities obtained via find(), getReference() or navigated to via
+ * relation traversal refers to the same instances.
+ * 
+ * Original reported in the context of entities of a inheritance hierarchy with
+ * SINGLE_TABLE strategy.
+ * 
+ * <A HREF="http://issues.apache.org/jira/browse/OPENJPA-677";>OPENJPA-677</A>
+ * 
+ * @author Przemek Koprowski
+ * @author Pinaki Poddar
+ * 
+ */
+public class TestIdentityWithSingleTableStrategy extends SingleEMFTestCase {
+       private Admin admin;
+       private RegularUser user;
+
+       public void setUp() {
+               super.setUp(CLEAR_TABLES, Admin.class, RegularUser.class,
+                               ComputerUser.class);
+
+               EntityManager em = emf.createEntityManager();
+               em.getTransaction().begin();
+               admin = new Admin();
+               user = new RegularUser();
+               user.setAdmin(admin);
+               admin.addRegularUser(user);
+               em.persist(admin);
+               em.persist(user);
+               em.getTransaction().commit();
+               em.close();
+       }
+
+       @Override
+       public void tearDown() {
+               // problem deleting table in MySQL
+       }
+
+       public void testFindAndNaviagtedEntityIdential() {
+               EntityManager em1 = emf.createEntityManager();
+               RegularUser regularUserFromFind = (RegularUser) em1.find(
+                               RegularUser.class, user.getOid());
+               Admin adminFromFind = em1.find(Admin.class, admin.getOid());
+               Admin adminFromMethodBean = regularUserFromFind.getAdmin();
+               assertTrue(adminFromFind == adminFromMethodBean);
+               em1.close();
+       }
+
+       public void testReferenceAndNaviagtedEntityIdential() {
+               EntityManager em1 = emf.createEntityManager();
+               RegularUser regularUserFromFind = (RegularUser) em1.find(
+                               RegularUser.class, user.getOid());
+               Admin adminFromGetReference = em1.getReference(Admin.class, 
admin
+                               .getOid());
+               Admin adminFromMethodBean = regularUserFromFind.getAdmin();
+               assertTrue(adminFromGetReference == adminFromMethodBean);
+               em1.close();
+       }
+}

Propchange: 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java?rev=1086217&view=auto
==============================================================================
--- 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
 (added)
+++ 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
 Mon Mar 28 12:54:36 2011
@@ -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.inheritance.entity;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.OneToMany;
+
+@Entity
+@DiscriminatorValue("admin")
+public class Admin extends ComputerUser {
+       @OneToMany(mappedBy = "admin")
+       protected Set<RegularUser> regularUsers = new HashSet<RegularUser>();
+
+       public boolean addRegularUser(RegularUser version) {
+               return regularUsers.add(version);
+       }
+
+}

Propchange: 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java?rev=1086217&view=auto
==============================================================================
--- 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
 (added)
+++ 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
 Mon Mar 28 12:54:36 2011
@@ -0,0 +1,40 @@
+/*
+ * 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.inheritance.entity;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+
+@Entity
+@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
+public abstract class ComputerUser {
+
+       @GeneratedValue(strategy = GenerationType.AUTO)
+       @Id
+       private Integer oid;
+
+       public Integer getOid() {
+               return oid;
+       }
+
+}

Propchange: 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java?rev=1086217&view=auto
==============================================================================
--- 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
 (added)
+++ 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
 Mon Mar 28 12:54:36 2011
@@ -0,0 +1,43 @@
+/*
+ * 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.inheritance.entity;
+
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.ManyToOne;
+
+import org.apache.openjpa.persistence.jdbc.Nonpolymorphic;
+
+@Entity
+@DiscriminatorValue("user")
+public class RegularUser extends ComputerUser {
+
+       @ManyToOne(fetch = FetchType.LAZY)
+       private Admin admin;
+
+       public Admin getAdmin() {
+               return admin;
+       }
+
+       public void setAdmin(Admin admin) {
+               this.admin = admin;
+       }
+
+}

Propchange: 
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to