diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java index 337b30dc0..79e1bb661 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java @@ -300,8 +300,8 @@ public class SelectConstructor { } // add conditions limiting the projections to the proper classes; if - // this isn't a projection then they will already be added - if (exps.projections.length > 0) { + // this isn't a projection or a subq then they will already be added + if (exps.projections.length > 0 || sel.getParent() != null) { ctx.store.loadSubclasses(mapping); mapping.getDiscriminator().addClassConditions((inner != null) ? inner : sel, subclasses, joins); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/TestDiscriminatorTypes.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/TestDiscriminatorTypes.java index baa7a6225..f0a5d1510 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/TestDiscriminatorTypes.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/TestDiscriminatorTypes.java @@ -18,7 +18,9 @@ */ package org.apache.openjpa.persistence.discriminator; +import java.util.List; import javax.persistence.EntityManager; +import javax.persistence.Query; import org.apache.openjpa.jdbc.meta.ClassMapping; import org.apache.openjpa.jdbc.meta.Discriminator; @@ -32,7 +34,7 @@ public class TestDiscriminatorTypes extends SingleEMFTestCase { CharRootEntity.class, IntegerAbstractEntity.class, IntegerLeafEntity.class, IntegerRootEntity.class, StringAbstractEntity.class, StringLeafEntity.class, - StringRootEntity.class); + StringRootEntity.class, CLEAR_TABLES, "openjpa.Log", "SQL=TRACE"); } public void testCharDiscriminators() { @@ -144,4 +146,33 @@ public class TestDiscriminatorTypes extends SingleEMFTestCase { assertNotNull(root2); em.close(); } + + public void testExistsQuery() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + + StringRootEntity e = new StringRootEntity(); + e.setName("foo"); + em.persist(e); + + e = new StringRootEntity(); + e.setName("foo"); + em.persist(e); + + e = new StringRootEntity(); + e.setName("bar"); + em.persist(e); + + em.getTransaction().commit(); + em.close(); + + em = emf.createEntityManager(); + Query q = em.createQuery("select o from StringAbstractEntity o " + + "where exists (select o2 from StringLeafEntity o2)"); + List list = q.getResultList(); + assertEquals(0, list.size()); + for (StringAbstractEntity entity : list) + assertTrue(entity instanceof StringLeafEntity); + em.close(); + } }