mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 19:00:38 +00:00
#969 - ENH: Add Finder.query(String ormQuery) ... helper method
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user