mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 23:56:36 +00:00
Fix for #223 - RawSql that includes a OneToMany not working
This commit is contained in:
@@ -225,6 +225,10 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
|
||||
return createElPropertyValue(propName, remainder, chain, propertyDeploy);
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
|
||||
// do not add to the selectChain at the top level of the Many bean
|
||||
}
|
||||
|
||||
public SqlUpdate deleteByParentId(Object parentId, List<Object> parentIdist) {
|
||||
if (parentId != null) {
|
||||
return deleteByParentId(parentId);
|
||||
|
||||
@@ -287,7 +287,6 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return true if this a OneToOne property. Otherwise assumed ManyToOne.
|
||||
*/
|
||||
|
||||
@@ -80,6 +80,9 @@ public class SqlTree {
|
||||
this.includes = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a select expression chain for RawSql.
|
||||
*/
|
||||
public List<String> buildSelectExpressionChain() {
|
||||
ArrayList<String> list = new ArrayList<String>();
|
||||
rootNode.buildSelectExpressionChain(list);
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.avaje.tests.rawsql;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.RawSql;
|
||||
import com.avaje.ebean.RawSqlBuilder;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestRawSqlMasterDetail extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String rs = "select t0.id, t0.status, t1.id, t1.name, "+
|
||||
" t2.id, t2.order_qty, t3.id, t3.name " +
|
||||
"from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id " +
|
||||
"join o_order_detail t2 on t2.order_id = t0.id " +
|
||||
"join o_product t3 on t3.id = t2.product_id " +
|
||||
"where t0.id <= :maxOrderId and t3.id = :productId "+
|
||||
"order by t0.id, t2.id asc";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(rs)
|
||||
.columnMapping("t0.id", "id")
|
||||
.columnMapping("t0.status", "status")
|
||||
.columnMapping("t1.id", "customer.id")
|
||||
.columnMapping("t1.name", "customer.name")
|
||||
.columnMapping("t2.id", "details.id")
|
||||
.columnMapping("t2.order_qty", "details.orderQty")
|
||||
.columnMapping("t3.id", "details.product.id")
|
||||
.columnMapping("t3.name", "details.product.name")
|
||||
.create();
|
||||
|
||||
List<Order> ordersFromRaw = Ebean.find(Order.class)
|
||||
.setRawSql(rawSql)
|
||||
.setParameter("maxOrderId", 2)
|
||||
.setParameter("productId", 1)
|
||||
.findList();
|
||||
|
||||
printOrders(ordersFromRaw, "using RawSql");
|
||||
|
||||
}
|
||||
|
||||
private void printOrders(List<Order> orders, String heading) {
|
||||
System.out.println("-------------- "+heading);
|
||||
for (Order order : orders) {
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
System.out.println("order: "+order.getId()+" "+order.getCustomer().getName());
|
||||
for (OrderDetail detail : details) {
|
||||
System.out.println("detailId:" + detail.getId() + " productId:" + detail.getProduct().getId() + " qty:" +
|
||||
detail.getOrderQty());
|
||||
}
|
||||
}
|
||||
System.out.println("-------------- "+heading+" complete");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user