mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
initial add of EbeanORM server based on v2.8.1
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.AdminAutofetch;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.bean.ObjectGraphNode;
|
||||
import com.avaje.ebean.bean.ObjectGraphOrigin;
|
||||
import com.avaje.ebean.meta.MetaAutoFetchStatistic;
|
||||
import com.avaje.ebean.meta.MetaAutoFetchStatistic.NodeUsageStats;
|
||||
import com.avaje.ebean.meta.MetaAutoFetchStatistic.QueryStats;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery;
|
||||
import com.avaje.tests.model.basic.Address;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestAutofetchTuneWithJoin extends TestCase {
|
||||
|
||||
public void test() {
|
||||
runQuery();
|
||||
collectUsage();
|
||||
}
|
||||
|
||||
private void runQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> q = Ebean.find(Order.class)
|
||||
.setAutofetch(true)
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.where().lt("id", 3)
|
||||
.query();
|
||||
|
||||
List<Order> list = q.findList();
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Order order = list.get(i);
|
||||
order.getOrderDate();
|
||||
order.getShipDate();
|
||||
// order.setShipDate(new Date(System.currentTimeMillis()));
|
||||
Customer customer = order.getCustomer();
|
||||
customer.getName();
|
||||
Address shippingAddress = customer.getShippingAddress();
|
||||
if (shippingAddress != null) {
|
||||
shippingAddress.getLine1();
|
||||
shippingAddress.getCity();
|
||||
}
|
||||
// customer.getContacts()
|
||||
}
|
||||
|
||||
SpiQuery<?> sq = (SpiQuery<?>) q;
|
||||
ObjectGraphNode parentNode = sq.getParentNode();
|
||||
ObjectGraphOrigin origin = parentNode.getOriginQueryPoint();
|
||||
|
||||
System.out.println("Origin:" + origin.getKey());
|
||||
|
||||
// MetaAutoFetchStatistic metaAutoFetchStatistic = ((DefaultOrmQuery<?>)q).getMetaAutoFetchStatistic();
|
||||
// if (metaAutoFetchStatistic != null) {
|
||||
// List<NodeUsageStats> nodeUsageStats = metaAutoFetchStatistic.getNodeUsageStats();
|
||||
// System.out.println(nodeUsageStats);
|
||||
// List<QueryStats> queryStats = metaAutoFetchStatistic.getQueryStats();
|
||||
// System.out.println(queryStats);
|
||||
// }
|
||||
|
||||
if (q.isAutofetchTuned()) {
|
||||
System.out.println("TUNED...");
|
||||
}
|
||||
}
|
||||
|
||||
private static void collectUsage() {
|
||||
|
||||
AdminAutofetch adminAutofetch = Ebean.getServer(null).getAdminAutofetch();
|
||||
adminAutofetch.collectUsageViaGC();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
|
||||
public class TestConnectionCloseOnSqlerr extends TestCase {
|
||||
|
||||
static boolean runManuallyNow = true;
|
||||
|
||||
public void test() {
|
||||
|
||||
if (runManuallyNow) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
try {
|
||||
Query<Customer> q0 = Ebean.find(Customer.class).where().icontains("namexxx", "Rob").query();
|
||||
|
||||
q0.findList();
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().contains("Unsuccessfully waited")) {
|
||||
fail("No connections found while only one thread is running. (after " + i + " queries)");
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testTransactional() {
|
||||
|
||||
if (runManuallyNow) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
try {
|
||||
Ebean.beginTransaction();
|
||||
Query<Customer> q0 = Ebean.find(Customer.class).where().icontains("namexxx", "Rob").query();
|
||||
|
||||
q0.findList();
|
||||
Ebean.commitTransaction();
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().contains("Unsuccessfully waited")) {
|
||||
fail("No connections found while only one thread is running. (after " + i + " queries)");
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} finally {
|
||||
if (Ebean.currentTransaction().isActive()) {
|
||||
Ebean.rollbackTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.BeanState;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.MyLobSize;
|
||||
|
||||
public class TestDefaultFetchLazy extends TestCase {
|
||||
|
||||
public void testFetchTypeLazy(){
|
||||
|
||||
MyLobSize m = new MyLobSize();
|
||||
m.setName("aname");
|
||||
m.setMyCount(10);
|
||||
m.setMyLob("A big lob of data");
|
||||
|
||||
Ebean.save(m);
|
||||
|
||||
Assert.assertNotNull(m.getId());
|
||||
|
||||
MyLobSize myLobSize = Ebean.find(MyLobSize.class, m.getId());
|
||||
|
||||
BeanState beanState = Ebean.getBeanState(myLobSize);
|
||||
Set<String> loadedProps = beanState.getLoadedProps();
|
||||
|
||||
Assert.assertNotNull(loadedProps);
|
||||
Assert.assertTrue(loadedProps.contains("id"));
|
||||
Assert.assertTrue(loadedProps.contains("name"));
|
||||
|
||||
// FetchType.LAZY properties excluded
|
||||
Assert.assertFalse(loadedProps.contains("myLob"));
|
||||
Assert.assertFalse(loadedProps.contains("myCount"));
|
||||
|
||||
|
||||
// the details is also tuned
|
||||
Query<MyLobSize> queryMany = Ebean.find(MyLobSize.class)
|
||||
.fetch("details")// ,"+query")
|
||||
.where().gt("id", 0).query();
|
||||
|
||||
queryMany.findList();
|
||||
|
||||
String generatedSql = queryMany.getGeneratedSql();
|
||||
Assert.assertTrue(generatedSql.contains("t1.other "));
|
||||
Assert.assertFalse(generatedSql.contains("t1.something "));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.Query.UseIndex;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestExprNestedDisjunction extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// java.sql.Date dateAfter = java.sql.Date.valueOf("2010-01-01");
|
||||
|
||||
java.sql.Date onAfter = java.sql.Date.valueOf("2009-08-31");
|
||||
|
||||
Query<Customer> q = Ebean.find(Customer.class)
|
||||
.setUseIndex(UseIndex.NO)
|
||||
.where()
|
||||
.disjunction()
|
||||
.conjunction()
|
||||
.startsWith("name", "r")
|
||||
.eq("anniversary", onAfter)
|
||||
.endJunction()
|
||||
.conjunction()
|
||||
.eq("status", Customer.Status.ACTIVE)
|
||||
.gt("id", 0)
|
||||
.endJunction()
|
||||
.orderBy().asc("name");
|
||||
|
||||
q.findList();
|
||||
String s = q.getGeneratedSql();
|
||||
|
||||
assertTrue(s.contains("(t0.name like ? and t0.anniversary = ? ) or (t0.status = ? and t0.id > ? )"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestIContains extends TestCase {
|
||||
|
||||
public void testIContains() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// case insensitive
|
||||
Query<Customer> q0 = Ebean.find(Customer.class)
|
||||
.where().icontains("name", "Rob")
|
||||
.query();
|
||||
|
||||
|
||||
q0.findList();
|
||||
String generatedSql = q0.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(generatedSql.contains("lower(t0.name)"));
|
||||
|
||||
|
||||
// not case insensitive
|
||||
q0 = Ebean.find(Customer.class)
|
||||
.where().contains("name", "Rob")
|
||||
.query();
|
||||
|
||||
|
||||
q0.findList();
|
||||
generatedSql = q0.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(generatedSql.contains(" t0.name "));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestInCollectionQueryPlan extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Object> idList1 = new ArrayList<Object>();
|
||||
idList1.add(1);
|
||||
|
||||
String oq = "find customer where id in (:idList)";
|
||||
Ebean.createQuery(Customer.class, oq)
|
||||
.setParameter("idList", idList1)
|
||||
.findList();
|
||||
|
||||
List<Object> idList2 = new ArrayList<Object>();
|
||||
idList2.add(1);
|
||||
idList2.add(2);
|
||||
|
||||
Ebean.createQuery(Customer.class, oq)
|
||||
.setParameter("idList", idList2)
|
||||
.findList();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.EOptOneA;
|
||||
|
||||
public class TestJoinOptOneCascade extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
// the left outer join cascades to the join for c
|
||||
Query<EOptOneA> query = Ebean.find(EOptOneA.class)
|
||||
.fetch("b")
|
||||
.fetch("b.c");
|
||||
|
||||
query.findList();
|
||||
String sql = query.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(sql.contains("left outer join eopt_one_b "));
|
||||
Assert.assertTrue(sql.contains("left outer join eopt_one_c "));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.OrderShipment;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestJoinQuery extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// test that join to order.details is not included
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.setAutofetch(false)
|
||||
.fetch("orders")
|
||||
.fetch("orders.details");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Assert.assertTrue("has rows", list.size() > 0);
|
||||
|
||||
// test that join to order.details is not included
|
||||
Query<OrderShipment> shipQuery = Ebean.find(OrderShipment.class)
|
||||
.setAutofetch(false)
|
||||
.fetch("order")
|
||||
.fetch("order.details");
|
||||
|
||||
|
||||
List<OrderShipment> shipList = shipQuery.findList();
|
||||
Assert.assertTrue("has rows", shipList.size() > 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestLimitAlterFetchMany extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// Due to the use of maxRows... we will convert
|
||||
// the fetch join to contacts over to a query join
|
||||
// ... otherwise we wouldn't be able to use the
|
||||
// limit offset clause
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
// this will automatically get converted to a
|
||||
// query join ... due to the maxRows
|
||||
.fetch("contacts")
|
||||
.setMaxRows(5);
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
System.out.println(list);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Expr;
|
||||
import com.avaje.ebean.Junction;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestLimitQuery extends TestCase {
|
||||
|
||||
public void testHasManyWithLimit()
|
||||
{
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setAutofetch(false);
|
||||
query.setFirstRow(0);
|
||||
query.setMaxRows(10);
|
||||
|
||||
Junction<Customer> junc = Expr.disjunction(query);
|
||||
junc.add(Expr.like("name", "%A%"));
|
||||
query.where(junc);
|
||||
|
||||
List<Customer> customer = query.findList();
|
||||
assertTrue(customer.size() > 0); // should at least find the "Cust NoAddress" customer
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestManyWhereJoin extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.select("id,status")
|
||||
//.join("orders")
|
||||
// the where on a 'many' (like orders) requires an
|
||||
// additional join and distinct which is independent
|
||||
// of a fetch join (if there is a fetch join)
|
||||
.where().eq("orders.status", Order.Status.NEW)
|
||||
//.where().eq("orders.details.product.name", "Desk")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
String sql = query.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(sql.indexOf("select distinct ") > -1);
|
||||
Assert.assertTrue(sql.indexOf("join o_order ") > -1);
|
||||
Assert.assertTrue(sql.indexOf(".status = ?") > -1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.MRole;
|
||||
import com.avaje.tests.model.basic.MUser;
|
||||
|
||||
public class TestManyWhereJoinM2M extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
Ebean.beginTransaction();
|
||||
|
||||
MRole r1 = new MRole();
|
||||
r1.setRoleName("role1");
|
||||
Ebean.save(r1);
|
||||
|
||||
MRole r2 = new MRole();
|
||||
r2.setRoleName("role2special");
|
||||
Ebean.save(r2);
|
||||
|
||||
MRole r3 = new MRole();
|
||||
r3.setRoleName("role3");
|
||||
Ebean.save(r3);
|
||||
|
||||
MUser u0 = new MUser();
|
||||
u0.setUserName("user0");
|
||||
u0.addRole(r1);
|
||||
u0.addRole(r2);
|
||||
|
||||
Ebean.save(u0);
|
||||
|
||||
MUser u1 = new MUser();
|
||||
u1.setUserName("user1");
|
||||
u1.addRole(r1);
|
||||
|
||||
Ebean.save(u1);
|
||||
|
||||
Ebean.commitTransaction();
|
||||
|
||||
|
||||
|
||||
Query<MUser> query = Ebean.find(MUser.class)
|
||||
.fetch("roles")
|
||||
// the where on a 'many' (like orders) requires an
|
||||
// additional join and distinct which is independent
|
||||
// of a fetch join (if there is a fetch join)
|
||||
.where().eq("roles.roleName", "role2special")
|
||||
.query();
|
||||
|
||||
List<MUser> list = query.findList();
|
||||
System.out.println(list);
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
Assert.assertTrue(sql.indexOf("select distinct") > -1);
|
||||
Assert.assertTrue(sql.indexOf("left outer join mrole ") > -1);
|
||||
Assert.assertTrue(sql.indexOf("join mrole ") > -1);
|
||||
Assert.assertTrue(sql.indexOf(".role_name = ?") > -1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.MyAdHoc;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestMyAdHocSqlSelect extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<MyAdHoc> list = Ebean.find(MyAdHoc.class)
|
||||
.where().gt("order_id", 0)
|
||||
.having().gt("detailCount", 0)
|
||||
.findList();
|
||||
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
|
||||
public class TestNoSpaceBracket extends TestCase {
|
||||
|
||||
|
||||
public void test() {
|
||||
|
||||
Query<Order> query = Ebean.createQuery(Order.class, "find order join customer(id,name) ");
|
||||
|
||||
query.findList();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.PagingList;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestPagingListLoop extends TestCase {
|
||||
|
||||
public void test() throws InterruptedException, ExecutionException {
|
||||
|
||||
// boolean autoRunTest = false;
|
||||
// if (!autoRunTest){
|
||||
// // we only want to run this test manually.
|
||||
// return;
|
||||
// }
|
||||
|
||||
try {
|
||||
ResetBasicData.reset();
|
||||
|
||||
for (int i = 0; i < 50; i++) {
|
||||
PagingList<Customer> pagingList = Ebean.find(Customer.class).findPagingList(10);
|
||||
pagingList.getFutureRowCount();//.get();
|
||||
|
||||
//createLeak();
|
||||
Thread.sleep(10);
|
||||
}
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// private void createLeak() {
|
||||
//
|
||||
// // create a transaction we never close ...
|
||||
// // ... a connection pool leak
|
||||
// Ebean.getServer(null).createTransaction();
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryFetchJoinWithOrder extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.fetch("details", new FetchConfig().query())
|
||||
.order().asc("id")
|
||||
.order().desc("details.id")
|
||||
.findList();
|
||||
|
||||
assertNotNull(list);
|
||||
|
||||
List<Order> list2 = Ebean.find(Order.class)
|
||||
.fetch("customer", new FetchConfig().query())
|
||||
.fetch("customer.contacts")
|
||||
.order().asc("id")
|
||||
.order().asc("customer.contacts.lastName")
|
||||
.findList();
|
||||
|
||||
assertNotNull(list2);
|
||||
|
||||
|
||||
List<Customer> list3 = Ebean.find(Customer.class)
|
||||
.fetch("orders")
|
||||
.filterMany("orders").eq("status", Order.Status.NEW)
|
||||
.order().desc("orders.id")
|
||||
.findList();
|
||||
|
||||
assertNotNull(list3);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.ContactNote;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryFetchSkipPath extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
|
||||
//.setAutofetch(true)
|
||||
.setAutofetch(false)
|
||||
.select("status")
|
||||
// .fetch("customer","id")
|
||||
// .fetch("customer.contacts","id")
|
||||
.fetch("customer.contacts.notes","title")
|
||||
.findList();
|
||||
|
||||
for (Order order : list) {
|
||||
order.getStatus();
|
||||
Customer customer = order.getCustomer();
|
||||
List<Contact> contacts = customer.getContacts();
|
||||
for (Contact contact : contacts) {
|
||||
System.out.println("contact:"+contact);
|
||||
List<ContactNote> notes = contact.getNotes();
|
||||
for (ContactNote contactNote : notes) {
|
||||
System.out.println("note:"+contactNote.getTitle());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryFilterMany extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.fetch("orders", new FetchConfig().lazy())
|
||||
.where().ilike("name","Rob%")
|
||||
.filterMany("orders").eq("status", Order.Status.NEW)
|
||||
.where().gt("id", 0)
|
||||
.query();
|
||||
|
||||
//query.filterMany("orders").eq("status", Order.Status.NEW);
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
for (Customer customer : list) {
|
||||
customer.getOrders().size();
|
||||
}
|
||||
|
||||
Customer c0 = list.get(0);
|
||||
System.out.println("......... refreshMany ...");
|
||||
Ebean.refreshMany(c0, "orders");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.MUser;
|
||||
|
||||
public class TestQueryFilterManyOnM2M extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
Ebean.find(MUser.class)
|
||||
.fetch("roles")
|
||||
.filterMany("roles").ilike("roleName","Jim%")
|
||||
.findList();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryFilterManySimple extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// not really last week :)
|
||||
Date lastWeek = Date.valueOf("2010-01-01");
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class)
|
||||
// .join("orders", new JoinConfig().lazy())
|
||||
// .join("orders", new JoinConfig().query())
|
||||
.fetch("orders")
|
||||
.fetch("contacts", new FetchConfig().query())
|
||||
.where().ilike("name","rob%")
|
||||
.filterMany("orders")
|
||||
.eq("status", Order.Status.NEW)
|
||||
.gt("orderDate", lastWeek)
|
||||
.filterMany("contacts").isNotNull("firstName")
|
||||
.findList();
|
||||
|
||||
System.out.println(list);
|
||||
// invoke lazy loading
|
||||
list.get(0).getOrders().size();
|
||||
System.out.println(list.get(0).getOrders());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.QueryIterator;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryFindIterate extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
Query<Customer> query = server.find(Customer.class)
|
||||
.setAutofetch(false)
|
||||
.fetch("contacts",new FetchConfig().query(2))
|
||||
.where().gt("id", 0)
|
||||
.orderBy("id")
|
||||
.setMaxRows(2);
|
||||
|
||||
int count = 0;
|
||||
|
||||
QueryIterator<Customer> it = query.findIterate();
|
||||
try {
|
||||
while (it.hasNext()) {
|
||||
Customer customer = it.next();
|
||||
customer.hashCode();
|
||||
count++;
|
||||
}
|
||||
} finally {
|
||||
it.close();
|
||||
}
|
||||
|
||||
Assert.assertEquals(2, count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryFindMapTypedKey extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<String, Customer> map =
|
||||
Ebean.find(Customer.class)
|
||||
.select("id, name")
|
||||
.findMap("name", String.class);
|
||||
|
||||
Assert.assertNotNull(map);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Article;
|
||||
import com.avaje.tests.model.basic.Section;
|
||||
|
||||
public class TestQueryFindReadOnly extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
Section s0 = new Section("some content");
|
||||
Article a0 = new Article("art1","auth1");
|
||||
a0.addSection(s0);
|
||||
|
||||
Ebean.save(a0);
|
||||
|
||||
Article ar1 = Ebean.find(Article.class)
|
||||
.setReadOnly(true)
|
||||
.setId(a0.getId())
|
||||
.findUnique();
|
||||
|
||||
Assert.assertNotNull(ar1);
|
||||
Assert.assertTrue("readonly",Ebean.getBeanState(ar1).isReadOnly());
|
||||
|
||||
List<Section> ar1sections = ar1.getSections();
|
||||
Assert.assertEquals(1, ar1sections.size());
|
||||
|
||||
Section s2 = ar1sections.get(0);
|
||||
Assert.assertTrue("readonly cascading",Ebean.getBeanState(s2).isReadOnly());
|
||||
|
||||
|
||||
Ebean.runCacheWarming(Article.class);
|
||||
|
||||
Article ar0 = Ebean.find(Article.class, a0.getId());
|
||||
|
||||
Assert.assertNotNull(ar0);
|
||||
Assert.assertFalse(Ebean.getBeanState(ar0).isReadOnly());
|
||||
|
||||
List<Section> ar0sections = ar0.getSections();
|
||||
Section s1 = ar0sections.get(0);
|
||||
Assert.assertFalse(Ebean.getBeanState(s1).isReadOnly());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.QueryResultVisitor;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryFindVisit extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
Query<Customer> query = server.find(Customer.class)
|
||||
.setAutofetch(false)
|
||||
.fetch("contacts",new FetchConfig().query(2))
|
||||
.where().gt("id", 0)
|
||||
.orderBy("id")
|
||||
.setMaxRows(2);
|
||||
|
||||
final AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
query.findVisit(new QueryResultVisitor<Customer>() {
|
||||
|
||||
public boolean accept(Customer bean) {
|
||||
counter.incrementAndGet();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
Assert.assertEquals(2, counter.get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryInAssocOne extends TestCase {
|
||||
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class)
|
||||
.where().lt("id",200)
|
||||
.findList();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.where().in("customer", list)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
String sql = query.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(sql, sql.indexOf("join o_customer t1 on t1.id = t0.kcustomer_id") > -1);
|
||||
Assert.assertTrue(sql, sql.indexOf("t0.kcustomer_id in (?") > -1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryInIdTypeConversion extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<String> idList = new ArrayList<String>();
|
||||
idList.add("1");
|
||||
idList.add("2");
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class)
|
||||
.where().idIn(idList)
|
||||
.findList();
|
||||
|
||||
assertNotNull(list);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryJoinManyNonRoot extends TestCase {
|
||||
|
||||
public void test_manyNonRoot() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> q = Ebean.find(Order.class)
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.where().gt("id", 0)
|
||||
.query();
|
||||
|
||||
List<Order> list = q.findList();
|
||||
String sql = q.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
Assert.assertTrue(sql.contains("join o_customer t1 on t1.id "));
|
||||
Assert.assertTrue(sql.contains("left outer join contact t2 on"));
|
||||
|
||||
}
|
||||
|
||||
public void test_manyRootQueryJoinOrderDetailsFirst() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> q = Ebean.find(Order.class)
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.where().gt("id", 0)
|
||||
.query();
|
||||
|
||||
List<Order> list = q.findList();
|
||||
String sql = q.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
Assert.assertTrue(sql.contains("join o_customer t1 on t1.id "));
|
||||
Assert.assertTrue(sql.contains("left outer join o_order_detail "));
|
||||
Assert.assertTrue(sql.contains("left outer join o_product "));
|
||||
|
||||
Assert.assertFalse(sql.contains("left outer join contact"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void test_manyRootQueryJoinCustomerContactsFirst() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> q = Ebean.find(Order.class)
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.where().gt("id", 0)
|
||||
.query();
|
||||
|
||||
List<Order> list = q.findList();
|
||||
String sql = q.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
Assert.assertTrue(sql.contains("join o_customer t1 on t1.id "));
|
||||
Assert.assertTrue(sql.contains("left outer join contact "));
|
||||
|
||||
Assert.assertFalse(sql.contains("left outer join o_order_detail "));
|
||||
Assert.assertFalse(sql.contains("left outer join o_product "));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryJoinMulipleMany extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.where().gt("id", 0)
|
||||
.findList();
|
||||
|
||||
Assert.assertNotNull(list);
|
||||
System.out.println(list);
|
||||
|
||||
for (Order order : list) {
|
||||
List<Contact> contacts = order.getCustomer().getContacts();
|
||||
contacts.size();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryJoinQueryNonRoot extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
BeanDescriptor<Order> d = server.getBeanDescriptor(Order.class);
|
||||
ElPropertyValue elGetValue = d.getElGetValue("customer.contacts");
|
||||
|
||||
Assert.assertTrue(elGetValue.containsMany());
|
||||
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts", "firstName", new FetchConfig().query().lazy(10))
|
||||
.fetch("customer.contacts.group")
|
||||
.where().lt("id", 3)
|
||||
.findList();
|
||||
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
|
||||
for (Order order : list) {
|
||||
List<Contact> contacts = order.getCustomer().getContacts();
|
||||
for (Contact contact : contacts) {
|
||||
contact.getFirstName();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// String oq = "find order join customer join customer.contacts join details (+query(4),+lazy(5))";
|
||||
// Query<Order> q = Ebean.createQuery(Order.class, oq);
|
||||
// q.setAutofetch(false);
|
||||
// List<Order> list2 = q.findList();
|
||||
//
|
||||
// Assert.assertTrue(list2.size() > 0);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryLimitOffsetSimple extends TestCase {
|
||||
|
||||
/**
|
||||
* Test the syntax of the limit offset clause.
|
||||
*/
|
||||
public void testMe() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.createQuery(Order.class, "where status = :A limit 100 offset 3");
|
||||
query.setParameter("A",Order.Status.NEW);
|
||||
|
||||
query
|
||||
.setFirstRow(10)
|
||||
//.setMaxRows(100)
|
||||
.findList();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryMultiManyOrder extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> q = Ebean.find(Order.class)
|
||||
.fetch("shipments")
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.where().gt("id", 0)
|
||||
.query();
|
||||
|
||||
List<Order> list = q.findList();
|
||||
String sql = q.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
Assert.assertTrue(sql.contains("join o_customer "));
|
||||
|
||||
Assert.assertFalse(sql.contains("left outer join contact "));
|
||||
Assert.assertFalse(sql.contains("left outer join o_order_detail "));
|
||||
Assert.assertFalse(sql.contains("left outer join o_product "));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryNullAssocOne extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
|
||||
Query<Order> q0 = Ebean.find(Order.class)
|
||||
.where().eq("customer", null).query();
|
||||
|
||||
List<Order> orders = q0.findList();
|
||||
assertNotNull(orders);
|
||||
assertTrue(q0.getGeneratedSql().contains("where t0.kcustomer_id is null"));
|
||||
|
||||
Query<Order> q1 = Ebean.find(Order.class)
|
||||
.where().eq("customer.id", null).query();
|
||||
|
||||
List<Order> o1 = q1.findList();
|
||||
assertTrue(o1.size() == orders.size());
|
||||
assertTrue(q1.getGeneratedSql().contains("where t0.kcustomer_id is null"));
|
||||
|
||||
|
||||
Query<Order> q2 = Ebean.find(Order.class)
|
||||
.where().isNull("customer").query();
|
||||
|
||||
List<Order> o2 = q2.findList();
|
||||
assertTrue(o2.size() == orders.size());
|
||||
assertTrue(q2.getGeneratedSql().contains("where t0.kcustomer_id is null"));
|
||||
|
||||
|
||||
Query<Order> q3 = Ebean.find(Order.class)
|
||||
.where().isNull("customer").query();
|
||||
|
||||
List<Order> o3 = q3.findList();
|
||||
assertTrue(o3.size() == orders.size());
|
||||
assertTrue(q3.getGeneratedSql().contains("where t0.kcustomer_id is null"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
|
||||
public class TestQueryParsing extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
String oq = "find order join customer join customer.contacts join details (+query(4),+lazy(5))";
|
||||
Query<Order> q = Ebean.createQuery(Order.class, oq);
|
||||
checkQuery(q);
|
||||
|
||||
String oq1 = "find order join customer join customer.contacts join details ( +query(4), +lazy(5) )";
|
||||
SpiQuery<?> q1 = (SpiQuery<?>)Ebean.createQuery(Order.class, oq1);
|
||||
checkQuery(q1);
|
||||
|
||||
String oq2 = "find order join customer join customer.contacts join details ( +query(4), +lazy(5) , *)";
|
||||
SpiQuery<?> q2 = (SpiQuery<?>)Ebean.createQuery(Order.class, oq2);
|
||||
checkQuery(q2);
|
||||
|
||||
String oq3 = "find order join customer join customer.contacts join details (+query(4),+lazy(5),*)";
|
||||
SpiQuery<?> q3 = (SpiQuery<?>)Ebean.createQuery(Order.class, oq3);
|
||||
checkQuery(q3);
|
||||
|
||||
String oq4 = "find order join customer join customer.contacts join details (+query(4) +lazy(5) *)";
|
||||
SpiQuery<?> q4 = (SpiQuery<?>)Ebean.createQuery(Order.class, oq4);
|
||||
checkQuery(q4);
|
||||
|
||||
}
|
||||
|
||||
private void checkQuery(Query<?> q){
|
||||
|
||||
|
||||
SpiQuery<?> sq = (SpiQuery<?>)q;
|
||||
OrmQueryDetail detail = sq.getDetail();
|
||||
|
||||
Assert.assertNotNull(detail.getChunk("customer", false));
|
||||
Assert.assertFalse(detail.getChunk("customer", false).isQueryFetch());
|
||||
Assert.assertFalse(detail.getChunk("customer", false).isLazyFetch());
|
||||
|
||||
Assert.assertNotNull(detail.getChunk("customer.contacts", false));
|
||||
Assert.assertFalse(detail.getChunk("customer.contacts", false).isQueryFetch());
|
||||
Assert.assertFalse(detail.getChunk("customer.contacts", false).isLazyFetch());
|
||||
|
||||
Assert.assertNotNull(detail.getChunk("details", false));
|
||||
Assert.assertTrue(detail.getChunk("details", false).isQueryFetch());
|
||||
Assert.assertTrue(detail.getChunk("details", false).isLazyFetch());
|
||||
|
||||
Assert.assertEquals(4, detail.getChunk("details", false).getQueryFetchBatch());
|
||||
Assert.assertEquals(5, detail.getChunk("details", false).getLazyFetchBatch());
|
||||
|
||||
Assert.assertTrue(detail.getChunk("details", false).allProperties());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryPathJoinAndOrder extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class)
|
||||
.select("id,name, status")
|
||||
.fetch("contacts")
|
||||
.order().asc("id")
|
||||
.order().desc("contacts.firstName")
|
||||
.setMaxRows(3)
|
||||
.findList();
|
||||
|
||||
assertNotNull(list);
|
||||
|
||||
// can't really assert that the contacts are batch loaded
|
||||
// via a secondary query join
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryPlanCacheRowCount extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.where()
|
||||
.eq("status", Order.Status.NEW)
|
||||
.ge("id", 1)
|
||||
.order().desc("id");
|
||||
|
||||
int rc0 = query.findRowCount();
|
||||
|
||||
List<Object> ids = query.findIds();
|
||||
Assert.assertEquals(rc0,ids.size());
|
||||
|
||||
List<Order> list0 = query.findList();
|
||||
Assert.assertEquals(rc0,list0.size());
|
||||
|
||||
int rc1 = query.findRowCount();
|
||||
Assert.assertEquals(rc0,rc1);
|
||||
|
||||
List<Object> ids1 = query.findIds();
|
||||
Assert.assertEquals(rc0,ids1.size());
|
||||
|
||||
List<Order> list1 = query.findList();
|
||||
Assert.assertEquals(rc0,list1.size());
|
||||
|
||||
int idGt = 5;
|
||||
if (ids1.size() > 0){
|
||||
Object id = ids.get(0);
|
||||
idGt = Integer.valueOf(""+id);
|
||||
}
|
||||
|
||||
// should still hit query plan cache
|
||||
Query<Order> query2 = Ebean.find(Order.class)
|
||||
.where()
|
||||
.eq("status", Order.Status.NEW)
|
||||
.ge("id", idGt)
|
||||
.order().desc("id");
|
||||
|
||||
int rc2 = query2.findRowCount();
|
||||
|
||||
System.out.println("Expection Not same "+rc0+" != "+rc2);
|
||||
Assert.assertNotSame(rc0, rc2);
|
||||
|
||||
List<Object> ids2 = query2.findIds();
|
||||
Assert.assertEquals(rc2,ids2.size());
|
||||
|
||||
List<Order> list2 = query2.findList();
|
||||
Assert.assertEquals(rc2,list2.size());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestRefToLazyLoadMany extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> custList = Ebean.find(Customer.class)
|
||||
.select("id")
|
||||
.findList();
|
||||
|
||||
Customer c = custList.get(0);
|
||||
|
||||
List<Contact> contacts2 = c.getContacts();
|
||||
assertEquals(3, Ebean.getBeanState(c).getLoadedProps().size());
|
||||
|
||||
// now lazy load the contacts
|
||||
contacts2.size();
|
||||
|
||||
|
||||
Customer c2 = Ebean.getReference(Customer.class, c.getId());
|
||||
|
||||
// we only "loaded" the contacts BeanList and not all of c2
|
||||
List<Contact> contacts = c2.getContacts();
|
||||
//Set<String> loadedProps = Ebean.getBeanState(c2).getLoadedProps();
|
||||
//assertEquals(1, loadedProps.size());
|
||||
|
||||
// now lazy load the contacts
|
||||
contacts.size();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestRowCount extends TestCase {
|
||||
|
||||
public void test(){
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.fetch("details")
|
||||
.where().gt("id", 1)
|
||||
.gt("details.id", 1)
|
||||
.order("id desc");
|
||||
|
||||
|
||||
int rc = query.findRowCount();
|
||||
System.out.println("rc:"+rc);
|
||||
|
||||
List<Object> ids = query.findIds();
|
||||
System.out.println("ids:"+ids);
|
||||
|
||||
List<Order> list = query.findList();
|
||||
System.out.println(list);
|
||||
|
||||
Assert.assertEquals("same rc to ids.size() ", rc, ids.size());
|
||||
Assert.assertEquals("same rc to list.size()", rc, list.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.CKeyParent;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import com.avaje.tests.model.basic.Vehicle;
|
||||
import com.avaje.tests.model.basic.VehicleDriver;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestSubQuery extends TestCase {
|
||||
|
||||
public void testId() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Integer> productIds = new ArrayList<Integer>();
|
||||
productIds.add(3);
|
||||
|
||||
Query<Order> sq = Ebean.createQuery(Order.class)
|
||||
.select("id")
|
||||
.where().in("details.product.id", productIds)
|
||||
.query();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.where().in("id", sq)
|
||||
.findList();
|
||||
|
||||
System.out.println(list);
|
||||
// FIXME: need to clear out old orders..
|
||||
//Assert.assertEquals(2,list.size());
|
||||
|
||||
String oq = " find order (id, status) where id in "
|
||||
+"(select a.id from o_order a join o_order_detail ad on ad.order_id = a.id where ad.product_id in (:prods)) ";
|
||||
|
||||
List<Order> list2 = Ebean.createQuery(Order.class, oq)
|
||||
.setParameter("prods", productIds)
|
||||
.findList();
|
||||
|
||||
System.out.println(list2);
|
||||
//Assert.assertEquals(2,list2.size());
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void testCompositeKey()
|
||||
{
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<CKeyParent> sq = Ebean.createQuery(CKeyParent.class)
|
||||
.select("id.oneKey")
|
||||
.setAutofetch(false)
|
||||
.where()
|
||||
.query();
|
||||
|
||||
Query<CKeyParent> pq = Ebean.find(CKeyParent.class)
|
||||
.where().in("id.oneKey", sq)
|
||||
.query();
|
||||
|
||||
pq.findList();
|
||||
|
||||
String sql = pq.getGeneratedSql();
|
||||
|
||||
String golden = "(t0.one_key) in (select t0.one_key from ckey_parent t0) ";
|
||||
|
||||
if (sql.indexOf(golden) < 0)
|
||||
{
|
||||
System.out.println("failed sql:"+sql);
|
||||
fail("golden string not found");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* show that ebean is not using the correct table name in the subquery (sq)
|
||||
*
|
||||
public void testInheritance1()
|
||||
{
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Vehicle> sq = Ebean.createQuery(Vehicle.class)
|
||||
.select("id")
|
||||
.setAutofetch(false)
|
||||
.where()
|
||||
.query();
|
||||
|
||||
Query<VehicleDriver> pq = Ebean.find(VehicleDriver.class)
|
||||
.where().in("vehicle.id", sq)
|
||||
.query();
|
||||
|
||||
pq.findList();
|
||||
|
||||
String sql = pq.getGeneratedSql();
|
||||
System.err.println(sql);
|
||||
|
||||
String golden = "(t0.vehicle_id) in (select t0.id from t0.vehicle t0)";
|
||||
if (sql.indexOf(golden) < 0)
|
||||
{
|
||||
System.out.println("failed sql:"+sql);
|
||||
fail("golden string not found");
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* show that ebean is adding the discriminator to the list of columns in the subquery
|
||||
*/
|
||||
public void testInheritance2()
|
||||
{
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<VehicleDriver> sq = Ebean.createQuery(VehicleDriver.class)
|
||||
.select("vehicle")
|
||||
.setAutofetch(false)
|
||||
.where()
|
||||
.query();
|
||||
|
||||
Query<Vehicle> pq = Ebean.find(Vehicle.class)
|
||||
.where().in("id", sq)
|
||||
.query();
|
||||
|
||||
pq.findList();
|
||||
|
||||
String sql = pq.getGeneratedSql();
|
||||
System.err.println(sql);
|
||||
|
||||
// TODO: If, after bugfixing, the system still join against vehicle I do not know now, in our case, it is not necessary if not
|
||||
// using it in the where clause
|
||||
String golden = "(t0.id) in (select t0.vehicle_id from vehicle_driver t0 left outer join vehicle t1 on t1.id = t0.vehicle_id )";
|
||||
if (sql.indexOf(golden) < 0)
|
||||
{
|
||||
System.out.println("failed sql:"+sql);
|
||||
fail("golden string not found");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* show that ebean is adding the discriminator to the list of columns in the subquery.
|
||||
* Second test to make sure that joining is still possible after bugfixing testInheritance2.
|
||||
*/
|
||||
public void testInheritance3()
|
||||
{
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<VehicleDriver> sq = Ebean.createQuery(VehicleDriver.class)
|
||||
.select("vehicle")
|
||||
.setAutofetch(false)
|
||||
.where()
|
||||
.eq("vehicle.licenseNumber", "abc")
|
||||
.query();
|
||||
|
||||
Query<Vehicle> pq = Ebean.find(Vehicle.class)
|
||||
.where().in("id", sq)
|
||||
.query();
|
||||
|
||||
pq.findList();
|
||||
|
||||
String sql = pq.getGeneratedSql();
|
||||
System.err.println(sql);
|
||||
|
||||
String golden = "(t0.id) in (select t0.vehicle_id from vehicle_driver t0 left outer join vehicle t1 on t1.id = t0.vehicle_id where t1.license_number = ? )";
|
||||
if (sql.indexOf(golden) < 0)
|
||||
{
|
||||
System.out.println("failed sql:"+sql);
|
||||
fail("golden string not found");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* show that ebean is using the wrong column (from the vehicle_driver table instead of vehicle) for the selected column in the subquery.
|
||||
* In contrast to testInheritance2+3 this test forces ebean to "drill down" to the key of the relation.
|
||||
*/
|
||||
public void testInheritance4()
|
||||
{
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<VehicleDriver> sq = Ebean.createQuery(VehicleDriver.class)
|
||||
.select("vehicle.id")
|
||||
.setAutofetch(false)
|
||||
.where()
|
||||
.query();
|
||||
|
||||
Query<Vehicle> pq = Ebean.find(Vehicle.class)
|
||||
.where().in("id", sq)
|
||||
.query();
|
||||
|
||||
pq.findList();
|
||||
|
||||
String sql = pq.getGeneratedSql();
|
||||
System.err.println(sql);
|
||||
|
||||
String golden = "(t0.id) in (select t0.vehicle_id from vehicle_driver t0 left outer join vehicle t1 on t1.id = t0.vehicle_id )";
|
||||
// OR without join
|
||||
// String golden = "(t0.id) in (select t0.vehicle_id from vehicle_driver t0)";
|
||||
if (sql.indexOf(golden) < 0)
|
||||
{
|
||||
System.out.println("failed sql:"+sql);
|
||||
fail("golden string not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestThreeLevelQueryJoin extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Ebean.find(Customer.class)
|
||||
.fetch("orders", new FetchConfig().query())
|
||||
.fetch("orders.details", new FetchConfig().query())
|
||||
.fetch("orders.shipments", new FetchConfig().query())
|
||||
.fetch("shippingAddress", new FetchConfig().query())
|
||||
.fetch("billingAddress", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Expr;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestWhereRawClause extends TestCase {
|
||||
|
||||
public void testRawClause() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
|
||||
Ebean.find(OrderDetail.class)
|
||||
.where()
|
||||
.not(Expr.eq("id", 1))
|
||||
.raw("orderQty < shipQty")
|
||||
.findList();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.avaje.tests.query.orderby;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestOrderByOnComplex extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.order().desc("customer");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
assertTrue(sql.contains("order by t0.kcustomer_id desc"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.avaje.tests.query.orderby;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestOrderByWithFunction extends TestCase {
|
||||
|
||||
public void testWithFunction() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.order("length(name),name");
|
||||
|
||||
query.findList();
|
||||
String sql = query.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(sql.indexOf("order by length(t0.name)") > -1);
|
||||
|
||||
|
||||
String oq = "find customer ORDER BY LENGTH(name),name";
|
||||
query = Ebean.createQuery(Customer.class, oq);
|
||||
query.findList();
|
||||
|
||||
sql = query.getGeneratedSql();
|
||||
Assert.assertTrue(sql.indexOf("order by LENGTH(t0.name)") > -1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package com.avaje.tests.query.orderby;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestOrderByWithMany extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
checkWithBuiltInMany();
|
||||
checkAppendId();
|
||||
checkNone();
|
||||
checkBoth();
|
||||
checkPrepend();
|
||||
checkAlreadyIncluded();
|
||||
checkAlreadyIncluded2();
|
||||
}
|
||||
|
||||
private void checkWithBuiltInMany() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.fetch("details")
|
||||
.order().desc("customer.name");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
|
||||
// t0.id inserted into the middle of the order by
|
||||
assertTrue(sql.contains("order by t1.name desc, t0.id, t2.id asc"));
|
||||
}
|
||||
|
||||
private void checkAppendId() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.fetch("shipments")
|
||||
.order().desc("customer.name");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
|
||||
// append the id to ensure ordering of root level objects
|
||||
assertTrue(sql.contains("order by t1.name desc, t0.id"));
|
||||
}
|
||||
|
||||
private void checkNone() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.order().desc("customer.name");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
|
||||
// no need to append id to order by as there is no 'many' included in the query
|
||||
assertTrue(sql.contains("order by t1.name desc"));
|
||||
assertTrue(!sql.contains("order by t1.name desc,"));
|
||||
}
|
||||
|
||||
private void checkBoth() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.fetch("shipments")
|
||||
.order().desc("customer.name, shipments.shipTime");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
// insert id into the middle of the order by
|
||||
assertTrue(sql.contains("order by t1.name, t0.id, t2.ship_time desc"));
|
||||
}
|
||||
|
||||
private void checkPrepend() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.fetch("shipments")
|
||||
.order().desc("shipments.shipTime");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
// prepend id in order by
|
||||
assertTrue(sql.contains("order by t0.id, t1.ship_time desc"));
|
||||
}
|
||||
|
||||
private void checkAlreadyIncluded() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.fetch("shipments")
|
||||
.order().desc("id, shipments.shipTime");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
// prepend id in order by
|
||||
assertTrue(sql.contains("order by t0.id, t1.ship_time desc"));
|
||||
}
|
||||
|
||||
|
||||
private void checkAlreadyIncluded2() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.fetch("shipments")
|
||||
.order().desc("orderDate, id, shipments.shipTime");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
// prepend id in order by
|
||||
assertTrue(sql.contains("order by t0.order_date, t0.id, t1.ship_time desc"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.avaje.tests.query.other;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.NoIdEntityType;
|
||||
|
||||
public class TestNoIdEntityType {
|
||||
|
||||
@Test
|
||||
public void testFindById() {
|
||||
|
||||
try {
|
||||
// this should fail for this entity
|
||||
Ebean.find(NoIdEntityType.class, 10);
|
||||
Assert.assertTrue(false);
|
||||
|
||||
} catch (IllegalStateException e){
|
||||
// expecting this exception
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
NoIdEntityType noId = new NoIdEntityType();
|
||||
noId.setName("foo");
|
||||
|
||||
Ebean.save(noId);
|
||||
|
||||
try {
|
||||
// this should fail as no @Id property
|
||||
Ebean.delete(noId);
|
||||
Assert.assertTrue(false);
|
||||
|
||||
} catch (IllegalStateException e){
|
||||
// expecting this exception
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user