You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by wi...@apache.org on 2007/05/04 00:49:50 UTC
svn commit: r535003 - in
/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test:
java/org/apache/openjpa/persistence/query/ resources/META-INF/
Author: wisneskid
Date: Thu May 3 15:49:49 2007
New Revision: 535003
URL: http://svn.apache.org/viewvc?view=rev&rev=535003
Log:
add query test for OPENJPA-51, queries with subselects with correlated/non-correlated aliases
Added:
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java
Modified:
incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java?view=auto&rev=535003
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/CustomerEntity.java Thu May 3 15:49:49 2007
@@ -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<OrderEntity> orders = new ArrayList<OrderEntity>();
+
+ 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<OrderEntity> getOrders() {
+ return orders;
+ }
+
+ public void setOrders(Collection<OrderEntity> 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;
+ }
+}
Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java?view=auto&rev=535003
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderEntity.java Thu May 3 15:49:49 2007
@@ -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 );
+ }
+}
Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java?view=auto&rev=535003
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java (added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubQuery.java Thu May 3 15:49:49 2007
@@ -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<query.length; i++) {
+ try {
+ List res = em.createQuery(query[i])
+ .getResultList();
+ } catch (Exception e) {
+ failures++;
+ }
+ }
+ em.getTransaction().begin();
+ try {
+ String update = "update Order o set o.amount = o.amount + 1 where "
+ + "o.oid not in (select o2.oid from Customer c, "
+ + "in(c.orders) o2)";
+ em.createQuery(update).executeUpdate();
+ }
+ catch (Exception e) {
+ failures++;
+ }
+ em.getTransaction().commit();
+ assertEquals(0, failures);
+ em.close();
+ }
+
+ public static void main(String[] args) {
+ TestRunner.run(TestSubQuery.class);
+ }
+}
+
Modified: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml?view=diff&rev=535003&r1=535002&r2=535003
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml (original)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml Thu May 3 15:49:49 2007
@@ -34,6 +34,8 @@
enhance time for XML data to get incorporated into PCRegistry
-->
<mapping-file>org/apache/openjpa/persistence/xml/orm.xml</mapping-file>
+ <class>org.apache.openjpa.persistence.query.CustomerEntity</class>
+ <class>org.apache.openjpa.persistence.query.OrderEntity</class>
<properties>
<!--
These properties are instead passed via System properties