OPENJPA-1885 Subquery restructure code

add more test string in TestSubquery

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@804037 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Catalina Wei 2009-08-13 21:47:39 +00:00
parent 06bd0d64e7
commit c9695c2fb9
3 changed files with 169 additions and 8 deletions

View File

@ -0,0 +1,81 @@
/*
* 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;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
/**
* Persistent class used in testing subquery.
*/
@Entity
@Table(name="ACCT")
public class Account {
@Id
@GeneratedValue
private long aid;
private int balance;
private Integer loan;
@ManyToOne
private Customer cust;
private String name;
public long getId() {
return aid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public Integer getLoan() {
return loan;
}
public void setLoan(Integer loan) {
this.loan = loan;
}
public Customer getCustomer() {
return cust;
}
public void setCustomer(Customer customer) {
this.cust = customer;
}
}

View File

@ -23,39 +23,40 @@ import javax.persistence.*;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
@Entity @Entity
@Table(name="TCTMR") @Table(name="TCUS")
public class Customer { public class Customer {
@Embeddable @Embeddable
public static class CustomerKey implements Serializable { public static class CustomerKey implements Serializable {
public String countryCode; public String cyCode;
public int id; public int id;
public CustomerKey(){} public CustomerKey(){}
public CustomerKey(String cc, int id){ public CustomerKey(String cc, int id){
countryCode=cc; cyCode=cc;
this.id=id; this.id=id;
} }
public String toString() { public String toString() {
return countryCode+"/"+id; return cyCode+"/"+id;
} }
@Override @Override
public boolean equals(Object obj){ public boolean equals(Object obj){
if (obj==this) return true; if (obj==this) return true;
if ( ! (obj instanceof CustomerKey) ) return false; if ( ! (obj instanceof CustomerKey) ) return false;
CustomerKey key = (CustomerKey)obj; CustomerKey key = (CustomerKey)obj;
if (key.countryCode.equals(this.countryCode) && if (key.cyCode.equals(this.cyCode) &&
key.id==this.id) return true; key.id==this.id) return true;
return false; return false;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return this.countryCode.hashCode() return this.cyCode.hashCode()
^ this.id; ^ this.id;
} }
} }
@ -76,6 +77,9 @@ public class Customer {
@OneToMany(fetch=FetchType.EAGER, mappedBy="customer") @OneToMany(fetch=FetchType.EAGER, mappedBy="customer")
private Collection<Order> orders = new ArrayList<Order>(); private Collection<Order> orders = new ArrayList<Order>();
@OneToMany(mappedBy="cust")
private List<Account> accounts = new ArrayList<Account>();
public Customer() {} public Customer() {}
public Customer(CustomerKey cid, String name, CreditRating rating){ public Customer(CustomerKey cid, String name, CreditRating rating){
@ -105,6 +109,14 @@ public class Customer {
this.orders = orders; this.orders = orders;
} }
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
public String toString() { public String toString() {
return "Customer:"+cid+" name:"+name; return "Customer:"+cid+" name:"+name;
} }

View File

@ -36,7 +36,7 @@ public class TestSubquery
public void setUp() { public void setUp() {
setUp(Customer.class, Customer.CustomerKey.class, Order.class, setUp(Customer.class, Customer.CustomerKey.class, Order.class,
OrderItem.class, Magazine.class, Publisher.class, Employee.class, OrderItem.class, Magazine.class, Publisher.class, Employee.class,
Dependent.class, DependentId.class, DROP_TABLES); Dependent.class, DependentId.class, Account.class, DROP_TABLES);
} }
static String[] querys = new String[] { static String[] querys = new String[] {
@ -160,6 +160,74 @@ public class TestSubquery
" where o.customer.cid.id = o2.customer.cid.id)", " where o.customer.cid.id = o2.customer.cid.id)",
}; };
static String[] querys2 = new String[] {
// 0
"select o1.oid, c.name from Order o1, Customer c" +
" where o1.customer.name = " +
" any(select o2.customer.name from in(c.orders) o2)",
// 1
"select o1.oid, c.name from Order o1, Customer c" +
" where o1.amount = " +
" any(select o2.amount from in(c.orders) o2)",
// 2
"select DISTINCT c.name FROM Customer c JOIN c.orders o " +
"WHERE EXISTS (SELECT o FROM o.lineitems l where l.quantity > 2 ) ",
// 3
"select DISTINCT c.name FROM Customer c, IN(c.orders) co " +
"WHERE co.amount > ALL " +
"(Select o.amount FROM Order o, in(o.lineitems) l WHERE l.quantity > 2)",
// 4
"select distinct c.name FROM Customer C, IN(C.orders) co " +
"WHERE co.amount < ALL " +
"(Select o.amount FROM Order o, IN(o.lineitems) l WHERE l.quantity > 2)",
//5
"select c.name FROM Customer c, IN(c.orders) co " +
"WHERE co.amount <= ALL " +
"(Select o.amount FROM Order o, IN(o.lineitems) l WHERE l.quantity > 2)",
// 6
"select DISTINCT c.name FROM Customer c, IN(c.orders) co " +
"WHERE co.amount > ANY " +
"(Select o.amount FROM Order o, IN(o.lineitems) l WHERE l.quantity = 2)",
// 7
"select DISTINCT c.name FROM Customer c " +
"WHERE EXISTS (SELECT o FROM c.orders o where o.amount " +
"BETWEEN 1000 AND 1200)",
// 8
"select DISTINCT c.name FROM Customer c " +
"WHERE EXISTS (SELECT o FROM c.orders o where o.amount > 1000 )",
// 9
"SELECT o.oid from Order o WHERE " +
"EXISTS (SELECT c.name From o.customer c WHERE c.name LIKE '%los') ",
// 10
"select Distinct c.name FROM Customer c, IN(c.orders) co " +
"WHERE co.amount >= SOME" +
"(Select o.amount FROM Order o, IN(o.lineitems) l WHERE l.quantity = 2)",
// 11
"select c FROM Customer c WHERE EXISTS" +
" (SELECT o FROM c.orders o where o.amount > 1000)",
// 12
"select c FROM Customer c WHERE EXISTS" +
" (SELECT o FROM c.orders o)",
// 13
"SELECT c FROM Customer c WHERE "
+ "(SELECT COUNT(o) FROM c.orders o) > 10",
// 14
"SELECT o FROM Order o JOIN o.customer c WHERE c.name = "
+ "SOME (SELECT a.name FROM c.accounts a)",
};
public void testSubquery2() {
EntityManager em = emf.createEntityManager();
for (int i = 0; i < querys2.length; i++) {
String q = querys2[i];
System.err.println(">>> JPQL JPA2 :[ " + i + "]" +q);
List rs = em.createQuery(q).getResultList();
assertEquals(0, rs.size());
}
em.close();
}
public void testSubquery() { public void testSubquery() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();