#969 - ENH: Add Finder.query(String ormQuery) ... helper method

This commit is contained in:
Rob Bygrave
2017-02-09 23:27:13 +13:00
parent 6744c7886a
commit d9e884b8bd
4 changed files with 30 additions and 4 deletions
+4 -4
View File
@@ -232,9 +232,9 @@ public interface EbeanServer {
*
* // Find order additionally fetching the customer, details and details.product name.
*
* String eql = "fetch customer fetch details fetch details.product (name) where id = :orderId ";
* String ormQuery = "fetch customer fetch details fetch details.product (name) where id = :orderId ";
*
* Query<Order> query = Ebean.createQuery(Order.class, eql);
* Query<Order> query = Ebean.createQuery(Order.class, ormQuery);
* query.setParameter("orderId", 2);
*
* Order order = query.findUnique();
@@ -251,11 +251,11 @@ public interface EbeanServer {
* }</pre>
*
* @param beanType The type of bean to fetch
* @param eql The Ebean query
* @param ormQuery The Ebean ORM query
* @param <T> The type of the entity bean
* @return The query with expressions defined as per the parsed query statement
*/
<T> Query<T> createQuery(Class<T> beanType, String eql);
<T> Query<T> createQuery(Class<T> beanType, String ormQuery);
/**
* Create a query for a type of entity bean.
+7
View File
@@ -163,4 +163,11 @@ public class Finder<I, T> {
public Query<T> nativeSql(String nativeSql) {
return db().findNative(type, nativeSql);
}
/**
* Creates a query using the ORM query language.
*/
public Query<T> query(String ormQuery) {
return db().createQuery(type, ormQuery);
}
}
@@ -28,6 +28,14 @@ public class CustomerFinder extends Finder<Integer, Customer> {
return query().where().eq("name", name).findUnique();
}
public List<Customer> byNameStatus(String nameStartsWith, Customer.Status status) {
return query("where status = :status and name istartsWith :name order by name")
.setParameter("status", status)
.setParameter("name", nameStartsWith)
.findList();
}
public List<String> namesStartingWith(String name) {
return nativeSql("select name from o_customer where name like ? order by name")
@@ -62,6 +62,17 @@ public class TestCustomerFinder extends BaseTestCase {
assertThat(notThere).isNull();
}
@Test
public void test_ormQuery() {
ResetBasicData.reset();
List<Customer> customers =
Customer.find.byNameStatus("R", Customer.Status.NEW);
assertThat(customers).isNotNull();
}
@Test
public void test_nativeSingleAttribute() {