Author: mikedd
Date: Thu Mar 11 02:25:11 2010
New Revision: 921665

URL: http://svn.apache.org/viewvc?rev=921665&view=rev
Log:
OPENJPA-1558:
merged to 1.3.x

Added:
    
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/
    
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Employee.java
    
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Manager.java
    
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Person.java
    
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/TestLazyFetch.java
Modified:
    
openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java

Modified: 
openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java?rev=921665&r1=921664&r2=921665&view=diff
==============================================================================
--- 
openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java
 (original)
+++ 
openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java
 Thu Mar 11 02:25:11 2010
@@ -107,15 +107,26 @@ public abstract class InValueDiscriminat
 
     public Class getClass(JDBCStore store, ClassMapping base, Result res)
         throws SQLException, ClassNotFoundException {
-        if (isFinal || !res.contains(disc.getColumns()[0])
-            || (base.getPCSuperclass() == null
-            && base.getJoinablePCSubclassMappings().length == 0))
+        if (isFinal 
+                || !useDiscrimColumn(base, res)
+                || (base.getPCSuperclass() == null && 
base.getJoinablePCSubclassMappings().length == 0)) {
             return base.getDescribedType();
+        }
 
-        Object cls =
-                res.getObject(disc.getColumns()[0], disc.getJavaType(), null);
+        Object cls = res.getObject(disc.getColumns()[0], disc.getJavaType(), 
null);
         return getClass(cls, store);
     }
+    
+    private final boolean useDiscrimColumn(ClassMapping base, Result res) 
throws SQLException {
+        if (res.getBaseMapping() != null && base != null) {
+            // check whether the result type is assignable to the base mapping.
+            // if not assignable the discriminator value will not be correct.
+            if 
(!base.getDescribedType().isAssignableFrom(res.getBaseMapping().getDescribedType()))
 {
+                return false;
+            }
+        }
+        return res.contains(disc.getColumns()[0]);
+    }
 
     public boolean hasClassConditions(ClassMapping base, boolean subclasses) {
         // if selecting the first mapped class and all subclasses, no need
@@ -161,3 +172,4 @@ public abstract class InValueDiscriminat
         return sql;
     }
 }
+

Added: 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Employee.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Employee.java?rev=921665&view=auto
==============================================================================
--- 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Employee.java
 (added)
+++ 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Employee.java
 Thu Mar 11 02:25:11 2010
@@ -0,0 +1,42 @@
+/*
+ * 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.discriminator.fetch;
+
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+
+...@entity(name="D_F_Employee")
+...@table(name="D_F_EMPLOYEE")
+...@discriminatorvalue(value="E")
+public class Employee extends Person {
+    @ManyToOne(fetch=FetchType.LAZY)
+    private Manager manager;
+
+    public Manager getManager() {
+        return manager;
+    }
+
+    public void setManager(Manager manager) {
+        this.manager = manager;
+    } 
+
+}

Added: 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Manager.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Manager.java?rev=921665&view=auto
==============================================================================
--- 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Manager.java
 (added)
+++ 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Manager.java
 Thu Mar 11 02:25:11 2010
@@ -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.discriminator.fetch;
+
+import java.util.Collection;
+
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+...@entity(name="D_F_Manager")
+...@table(name="D_F_MANAGER")
+...@discriminatorvalue(value="M")
+public class Manager extends Person {
+    @OneToMany(fetch=FetchType.LAZY,mappedBy="manager")
+    Collection<Employee> employees;
+
+    public Collection<Employee> getEmployees() {
+        return employees;
+    }
+
+    public void setEmployees(Collection<Employee> employees) {
+        this.employees = employees;
+    }
+}

Added: 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Person.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Person.java?rev=921665&view=auto
==============================================================================
--- 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Person.java
 (added)
+++ 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/Person.java
 Thu Mar 11 02:25:11 2010
@@ -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.discriminator.fetch;
+
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+import javax.persistence.Table;
+
+...@entity(name="D_F_Person")
+...@table(name="D_F_PERSON")
+...@inheritance(strategy = InheritanceType.JOINED)
+...@discriminatorvalue(value="P")
+public abstract class Person {
+    @Id
+    private int id;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+}

Added: 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/TestLazyFetch.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/TestLazyFetch.java?rev=921665&view=auto
==============================================================================
--- 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/TestLazyFetch.java
 (added)
+++ 
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/fetch/TestLazyFetch.java
 Thu Mar 11 02:25:11 2010
@@ -0,0 +1,112 @@
+/*
+ * 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.discriminator.fetch;
+
+import java.util.HashSet;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+
+import org.apache.openjpa.persistence.test.PersistenceTestCase;
+
+public class TestLazyFetch extends PersistenceTestCase {
+    private static int N_EMPS = 3;
+
+    public EntityManagerFactory newEmf() {
+        EntityManagerFactory emf = createEMF(Person.class, Employee.class, 
Manager.class);
+        assertNotNull("Unable to create EntityManagerFactory", emf);
+        return emf;
+    }
+
+    public void setUp() {
+        EntityManagerFactory emf = newEmf();
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+
+        // cleanup from previous execution
+        tran.begin();
+        em.createQuery("Delete from D_F_Manager").executeUpdate();
+        em.createQuery("Delete from D_F_Employee").executeUpdate();
+        tran.commit();
+
+        // populate a small graph.
+        tran.begin();
+        Manager m = new Manager();
+        m.setId(10);
+        m.setEmployees(new HashSet<Employee>());
+        em.persist(m);
+
+        Employee e;
+        for (int i = 0; i < N_EMPS; i++) {
+            e = new Employee();
+            e.setId(i + 1);
+            e.setManager(m);
+            m.getEmployees().add(e);
+            em.persist(e);
+        }
+        tran.commit();
+
+        em.close();
+        emf.close();
+    }
+
+    @SuppressWarnings("unchecked")
+    public void testFetchOneSideFirst() {
+        EntityManagerFactory emf = newEmf();
+        EntityManager em = emf.createEntityManager();
+
+        List<Manager> managers = em.createQuery("Select m from D_F_Manager 
m").getResultList();
+        assertEquals(1, managers.size());
+        Manager m = managers.get(0);
+        
+        List<Employee> emps = em.createQuery("Select e from D_F_Employee 
e").getResultList();
+        assertEquals(N_EMPS, emps.size());
+        
+        for(Employee e : emps) { 
+            assertNotNull(e.getManager());
+            assertTrue(m.getEmployees().contains(e));
+            assertEquals(m, e.getManager());
+        }
+        em.close();
+        emf.close();
+    }   
+    
+    @SuppressWarnings("unchecked")
+    public void testFetchManySideFirst() {
+        EntityManagerFactory emf = newEmf();
+        EntityManager em = emf.createEntityManager();
+        
+        List<Employee> emps = em.createQuery("Select e from D_F_Employee 
e").getResultList();
+        assertEquals(N_EMPS, emps.size());
+        
+        List<Manager> managers = em.createQuery("Select m from D_F_Manager 
m").getResultList();
+        assertEquals(1, managers.size());
+        Manager m = managers.get(0);
+        
+        for(Employee e : emps) { 
+            assertNotNull(e.getManager());
+            assertTrue(m.getEmployees().contains(e));
+            assertEquals(m, e.getManager());
+        }
+        em.close();
+        emf.close();
+    }
+}


Reply via email to