Author: pcl
Date: Thu Jan 31 09:23:34 2008
New Revision: 617164

URL: http://svn.apache.org/viewvc?rev=617164&view=rev
Log:
OPENJPA-135, OPENJPA-420

Added:
    
openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDistinctQueries.java
Modified:
    
openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java

Modified: 
openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java?rev=617164&r1=617163&r2=617164&view=diff
==============================================================================
--- 
openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
 (original)
+++ 
openjpa/branches/1.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/jpql/JPQLExpressionBuilder.java
 Thu Jan 31 09:23:34 2008
@@ -383,6 +383,13 @@
 
         JPQLNode selectNode = root();
 
+        JPQLNode selectClause = selectNode.
+            findChildByID(JJTSELECTCLAUSE, false);
+        if (selectClause != null && selectClause.hasChildID(JJTDISTINCT))
+            exps.distinct = exps.DISTINCT_TRUE | exps.DISTINCT_AUTO;
+        else
+            exps.distinct = exps.DISTINCT_FALSE;
+
         JPQLNode constructor = selectNode.findChildByID(JJTCONSTRUCTOR, true);
         if (constructor != null) {
             // build up the fully-qualified result class name by
@@ -391,16 +398,8 @@
             exps.resultClass = resolver.classForName(resultClassName, null);
 
             // now assign the arguments to the select clause as the projections
-            exps.distinct = exps.DISTINCT_FALSE;
             return assignProjections(right(constructor), exps);
         } else {
-            JPQLNode selectClause = selectNode.
-                findChildByID(JJTSELECTCLAUSE, false);
-            if (selectClause != null && selectClause.hasChildID(JJTDISTINCT))
-                exps.distinct = exps.DISTINCT_TRUE | exps.DISTINCT_AUTO;
-            else
-                exps.distinct = exps.DISTINCT_FALSE;
-
             // handle SELECT clauses
             JPQLNode expNode = selectNode.
                 findChildByID(JJTSELECTEXPRESSIONS, true);

Added: 
openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDistinctQueries.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDistinctQueries.java?rev=617164&view=auto
==============================================================================
--- 
openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDistinctQueries.java
 (added)
+++ 
openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestDistinctQueries.java
 Thu Jan 31 09:23:34 2008
@@ -0,0 +1,98 @@
+/*
+ * 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.query;
+
+import java.util.List;
+
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
+import org.apache.openjpa.persistence.models.company.fetchlazy.ProductOrder;
+import org.apache.openjpa.persistence.models.company.fetchlazy.LineItem;
+import org.apache.openjpa.persistence.models.company.fetchlazy.Product;
+import org.apache.openjpa.persistence.models.company.fetchlazy.Customer;
+import org.apache.openjpa.persistence.models.company.fetchlazy.Address;
+import org.apache.openjpa.persistence.models.company.fetchlazy.Company;
+import org.apache.openjpa.persistence.models.company.fetchlazy.Employee;
+import 
org.apache.openjpa.persistence.models.company.fetchlazy.PartTimeEmployee;
+import 
org.apache.openjpa.persistence.models.company.fetchlazy.FullTimeEmployee;
+import org.apache.openjpa.persistence.models.company.fetchlazy.Person;
+
+public class TestDistinctQueries extends SingleEMTestCase {
+
+    public void setUp() {
+        setUp(Address.class, Company.class, Customer.class, Employee.class,
+            FullTimeEmployee.class, LineItem.class, PartTimeEmployee.class,
+            Person.class, Product.class, ProductOrder.class, CLEAR_TABLES);
+
+        ProductOrder order = new ProductOrder();
+        LineItem item0 = new LineItem();
+        LineItem item1 = new LineItem();
+        LineItem item2 = new LineItem();
+        order.getItems().add(item0);
+        order.getItems().add(item1);
+        order.getItems().add(item2);
+
+        em.getTransaction().begin();
+        em.persist(order);
+        em.persist(item0);
+        em.persist(item1);
+        em.persist(item2);
+        em.getTransaction().commit();
+    }
+
+    public void testDuplicateResultsInNonDistinctJoinFetchQuery() {
+        List l = em.createQuery("select o from LAZ_ProductOrder o " +
+            "left join fetch o.items").getResultList();
+        assertEquals(3, l.size());
+    }
+
+    public void testDuplicateResultsInNonDistinctJoinQuery() {
+        List l = em.createQuery("select o from LAZ_ProductOrder o " +
+            "left join o.items item").getResultList();
+        assertEquals(3, l.size());
+    }
+
+    public void testNoDuplicateResultsInDistinctQuery() {
+        List l = em.createQuery("select distinct o from LAZ_ProductOrder o " +
+            "left join o.items item").getResultList();
+        assertEquals(1, l.size());
+    }
+
+    public void testDuplicateResultsInNonDistinctConstructorJoinQuery() {
+        List l = em.createQuery("select new " +
+            "org.apache.openjpa.persistence.query.TestDistinctQueries$Holder(" 
+
+            "o.id) from LAZ_ProductOrder o " +
+            "left join o.items item").getResultList();
+        assertEquals(3, l.size());
+    }
+
+    public void testNoDuplicateResultsInDistinctConstructorQuery()
+        throws NoSuchMethodException {
+        List l = em.createQuery("select distinct new " +
+            "org.apache.openjpa.persistence.query.TestDistinctQueries$Holder(" 
+
+            "o.id) from LAZ_ProductOrder o " +
+            "left join o.items item").getResultList();
+        assertEquals(1, l.size());
+    }
+
+    public static class Holder {
+        public Holder(long id) {
+            // we don't actually do anything with the returned data
+        }
+    }
+}


Reply via email to