mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#765 - ENH: Add ebean.xml with support for named RawSql, also adds EbeanServer.createNamedQuery()
This commit is contained in:
@@ -285,6 +285,11 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Query<T> createNamedQuery(Class<T> beanType, String namedQuery) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Query<T> createQuery(Class<T> beanType) {
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.avaje.ebeaninternal.xmlmapping.model;
|
||||
|
||||
import com.avaje.ebeaninternal.xmlmapping.XmlMappingReader;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class XmlMappingReaderTest {
|
||||
|
||||
@Test
|
||||
public void read() throws Exception {
|
||||
|
||||
InputStream is = XmlMappingReaderTest.class.getResourceAsStream("/test-ebean.xml");
|
||||
XmEbean testMapping = XmlMappingReader.read(is);
|
||||
|
||||
assertNotNull(testMapping);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import com.avaje.tests.model.basic.OrderAggregate;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestOrderTotalAmountReportBean extends BaseTestCase {
|
||||
|
||||
@@ -73,5 +74,61 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultNamedRawSql() {
|
||||
|
||||
Query<OrderAggregate> query = Ebean.find(OrderAggregate.class);
|
||||
List<OrderAggregate> list = query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount");
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedRawSql() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<OrderAggregate> query = Ebean.getDefaultServer().createNamedQuery(OrderAggregate.class, "withMax");
|
||||
List<OrderAggregate> list = query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount, max(order_qty*unit_price) as maxAmount from o_order_detail");
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedRawSql_with_extraPredicates() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<OrderAggregate> query = Ebean.getDefaultServer().createNamedQuery(OrderAggregate.class, "withMax");
|
||||
List<OrderAggregate> list = query
|
||||
.where().gt("order.id", 1)
|
||||
.having().gt("totalItems", 1)
|
||||
.order().desc("totalAmount")
|
||||
.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount, max(order_qty*unit_price) as maxAmount from o_order_detail");
|
||||
assertThat(query.getGeneratedSql()).contains("from o_order_detail where order_id > ? group by order_id having count(*) > ? order by sum(order_qty*unit_price) desc");
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedRawSql_with_param() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<OrderAggregate> query = Ebean.getDefaultServer().createNamedQuery(OrderAggregate.class, "withParam");
|
||||
List<OrderAggregate> list = query
|
||||
.setParameter("minId", 2)
|
||||
.where().isNotNull("order.id")
|
||||
.having().lt("totalAmount", 100)
|
||||
.order().desc("totalAmount")
|
||||
.setMaxRows(10)
|
||||
.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount, max(order_qty*unit_price) as maxAmount from o_order_detail");
|
||||
assertThat(query.getGeneratedSql()).contains("from o_order_detail where id > ? and order_id is not null group by order_id having sum(order_qty*unit_price) < ? order by sum(order_qty*unit_price) desc");
|
||||
assertNotNull(list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,11 @@ public class OrderAggregate {
|
||||
@OneToOne
|
||||
Order order;
|
||||
|
||||
Double maxAmount;
|
||||
|
||||
Double totalAmount;
|
||||
|
||||
Double totalItems;
|
||||
Long totalItems;
|
||||
|
||||
public String toString() {
|
||||
return order.getId() + " totalAmount:" + totalAmount + " totalItems:" + totalItems;
|
||||
@@ -43,11 +45,19 @@ public class OrderAggregate {
|
||||
this.totalAmount = totalAmount;
|
||||
}
|
||||
|
||||
public Double getTotalItems() {
|
||||
public Long getTotalItems() {
|
||||
return totalItems;
|
||||
}
|
||||
|
||||
public void setTotalItems(Double totalItems) {
|
||||
public void setTotalItems(Long totalItems) {
|
||||
this.totalItems = totalItems;
|
||||
}
|
||||
|
||||
public Double getMaxAmount() {
|
||||
return maxAmount;
|
||||
}
|
||||
|
||||
public void setMaxAmount(Double maxAmount) {
|
||||
this.maxAmount = maxAmount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,25 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testNamed() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.createNamedQuery(Order.class, "myRawTest");
|
||||
query.setParameter("orderStatus", Order.Status.NEW);
|
||||
query.setMaxRows(10);
|
||||
List<Order> list = query.findList();
|
||||
for (Order order : list) {
|
||||
order.getCretime();
|
||||
}
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
assertThat(sql).contains("select o.id, o.status, o.ship_date, c.id, c.name, a.id, a.line_1, a.line_2, a.city from o_order o");
|
||||
assertThat(sql).contains("join o_customer c on o.kcustomer_id = c.id ");
|
||||
assertThat(sql).contains("where o.status = ? order by c.name, c.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user