openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpala...@apache.org
Subject svn commit: r1172200 - in /openjpa/branches/1.1.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/per...
Date Sun, 18 Sep 2011 05:34:46 GMT
Author: rpalache
Date: Sun Sep 18 05:34:46 2011
New Revision: 1172200

URL: http://svn.apache.org/viewvc?rev=1172200&view=rev
Log:
OPENJPA-677: Make a judgement whether reading discrimnator value from the result makes sense
in a context. Bakporting Pinaki's fix to 1.1.x

Added:
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
Modified:
    openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java

Modified: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java?rev=1172200&r1=1172199&r2=1172200&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
(original)
+++ openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java
Sun Sep 18 05:34:46 2011
@@ -171,6 +171,7 @@ public class ClassMapping
         FieldMapping fm;
         Joinable join;
         int pkIdx;
+        boolean canReadDiscriminator = true;
         for (int i = 0; i < pks.length; i++) {
             // we know that all pk column join mappings use primary key fields,
             // cause this mapping uses the oid as its primary key (we recursed
@@ -179,6 +180,7 @@ public class ClassMapping
             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);
@@ -193,8 +195,12 @@ 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);
@@ -214,6 +220,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
      * columns will be primary key columns of this mapping, but may be in

Added: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java?rev=1172200&view=auto
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
(added)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/TestIdentityWithSingleTableStrategy.java
Sun Sep 18 05:34:46 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();
+	}
+}

Added: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java?rev=1172200&view=auto
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
(added)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/Admin.java
Sun Sep 18 05:34:46 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);
+	}
+
+}

Added: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java?rev=1172200&view=auto
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
(added)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/ComputerUser.java
Sun Sep 18 05:34:46 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;
+	}
+
+}

Added: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java?rev=1172200&view=auto
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
(added)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/inheritance/entity/RegularUser.java
Sun Sep 18 05:34:46 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;
+	}
+
+}



Mime
View raw message