mirror of https://github.com/apache/openjpa.git
OPENJPA-135, OPENJPA-420 Merge from ../branches/1.0.x. svn merge -c 617164
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@617200 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
171dc5d46a
commit
64d0ea8166
|
@ -395,6 +395,13 @@ public class JPQLExpressionBuilder
|
|||
|
||||
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
|
||||
|
@ -403,16 +410,8 @@ public class JPQLExpressionBuilder
|
|||
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);
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue