From bcc3b67223feefb2b379df3afb66295555f4a5c1 Mon Sep 17 00:00:00 2001 From: "David J. Wisneski" Date: Thu, 3 May 2007 22:49:49 +0000 Subject: [PATCH] add query test for OPENJPA-51, queries with subselects with correlated/non-correlated aliases git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@535003 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/query/CustomerEntity.java | 87 +++++++++++++++++ .../persistence/query/OrderEntity.java | 80 ++++++++++++++++ .../persistence/query/TestSubQuery.java | 94 +++++++++++++++++++ .../test/resources/META-INF/persistence.xml | 2 + 4 files changed, 263 insertions(+) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java new file mode 100644 index 000000000..594b66844 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java @@ -0,0 +1,87 @@ +/* + * 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 javax.persistence.*; + +import java.util.Collection; +import java.util.ArrayList; + +@Entity(name="Customer") +@Table(name="CUSTOMERTB") +public class CustomerEntity { + + public enum CreditRating { POOR, GOOD, EXCELLENT }; + + @Id + @GeneratedValue + long cid; + @Column(length=30) + String name; + @Enumerated + CreditRating creditRating; + @Version + long version; + + @OneToMany(fetch=FetchType.LAZY, mappedBy="customer") + private Collection orders = new ArrayList(); + + public CustomerEntity() {} + + public CustomerEntity(String name, CreditRating rating){ + this.name=name; + this.creditRating=rating; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public CreditRating getRating() { + return creditRating; + } + + public void setRating(CreditRating rating) { + this.creditRating = rating; + } + + public Collection getOrders() { + return orders; + } + + public void setOrders(Collection orders) { + this.orders = orders; + } + + public String toString() { + return "Customer:"+cid+" name:"+name; + } + + public long getCid() { + return cid; + } + + public void setCid(long cid) { + this.cid = cid; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java new file mode 100644 index 000000000..a09f3ecb0 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java @@ -0,0 +1,80 @@ +/* + * 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 javax.persistence.*; + +@Entity(name="Order") +@Table(name="ORDERTB") +public class OrderEntity { + @Id + @GeneratedValue + int oid; + + double amount; + boolean delivered; + + @ManyToOne + CustomerEntity customer; + + @Version + long version; + + public OrderEntity(){} + + public OrderEntity( double amt, boolean delivered, CustomerEntity c){ + amount=amt; + this.delivered=delivered; + customer=c; + if (c!=null) c.getOrders().add(this); + } + + public double getAmount() { + return amount; + } + + public void setAmount(double amount) { + this.amount = amount; + } + + public CustomerEntity getCustomer() { + return customer; + } + + public void setCustomer(CustomerEntity customer) { + this.customer = customer; + } + + public boolean isDelivered() { + return delivered; + } + + public void setDelivered(boolean delivered) { + this.delivered = delivered; + } + + public int getOid() { + return oid; + } + + public String toString(){ + return "Order:"+oid+" amount:"+amount+" delivered:"+delivered+" customer:"+ + ( customer!=null ? customer.getCid() : -1 ); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java new file mode 100644 index 000000000..f2eab7afc --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java @@ -0,0 +1,94 @@ +/* + * 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 javax.persistence.EntityManager; + +import junit.textui.TestRunner; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +/** + * Test queries with subselects + * + * @author Catalina Wei + */ +public class TestSubQuery + extends SingleEMFTestCase { + + public void setUp() { + setUp(CustomerEntity.class, CustomerEntity.class, + OrderEntity.class, OrderEntity.class); + } + + public void testQuery() { + EntityManager em = emf.createEntityManager(); + String[] query = new String[] { + "select o from Customer c, in(c.orders) o where o.amount > " + + "(select avg(o.amount) from Order o)", + "select c from Customer c, in(c.orders) o where o.amount > " + + "(select avg(o.amount) from in(c.orders) o)", + "select c from Customer c where " + + "((select sum(o.amount) from Order o where o.customer = c) " + + "between 100 and 200) order by c.name", + "select o from Order o where o.amount < " + + "(select max(o2.amount) from Order o2 where " + + "o2.amount = o.amount)", + "select o from Order o where o.amount > " + + "(select avg(o.amount) from Customer c, in(c.orders) o)", + "select o.oid from Order o where o.amount > 10 " + + "and o.amount < (select min(o2.amount) from Order o2 where " + + "o2.amount > 0)", + "select o from Order o where o.amount > any " + + "(select o.amount from Customer c, in (c.orders) o where " + + "c.cid = 1)", + "select o from Order o where o.amount between " + + "(select min(o.amount) from Customer c, in(c.orders) o) and " + + "(select avg(o.amount) from Customer c, in(c.orders) o)" + }; + + int failures = 0; + for (int i=0; i org/apache/openjpa/persistence/xml/orm.xml + org.apache.openjpa.persistence.query.CustomerEntity + org.apache.openjpa.persistence.query.OrderEntity