Object relational query for finding a List, Set, Map or single entity bean.
Example: Create the query using the API.
List orderList =
Example: The same query using the query language
String oql =
Example: Using a named query called "with.cust.and.details"
Query query = ebeanServer.createNamedQuery(Order.class,"with.cust.and.details");
AutoTune
Ebean has built in support for "AutoTune". This is a mechanism where a query
can be automatically tuned based on profiling information that is collected.
This is effectively the same as automatically using select() and fetch() to
build a query that will fetch all the data required by the application and no
more.
It is expected that AutoTune will be the default approach for many queries
in a system. It is possibly not as useful where the result of a query is sent
to a remote client or where there is some requirement for "Read Consistency"
guarantees.
Query Language
Partial Objects
The find and fetch clauses support specifying a list of
properties to fetch. This results in objects that are "partially populated".
If you try to get a property that was not populated a "lazy loading" query
will automatically fire and load the rest of the properties of the bean (This
is very similar behaviour as a reference object being "lazy loaded").
Partial objects can be saved just like fully populated objects. If you do
this you should remember to include the "Version" property in the
initial fetch. If you do not include a version property then optimistic
concurrency checking will occur but only include the fetched properties.
Refer to "ALL Properties/Columns" mode of Optimistic Concurrency checking.
[ select [ ( * | {fetch properties} ) ] ]
[ fetch {path} [ ( * | {fetch properties} ) ] ]
[ where {predicates} ]
[ order by {order by properties} ]
[ limit {max rows} [ offset {first row} ] ]
}
SELECT [ ( * | {fetch properties} ) ]
With the select you can specify a list of properties to fetch.
FETCH {path} [ ( * | {fetch properties} ) ]
With the fetch you specify the associated property to fetch and populate. The
path is a OneToOne, ManyToOne, OneToMany or ManyToMany property.
For fetch of a path we can optionally specify a list of properties to fetch.
If you do not specify a list of properties ALL the properties for that bean
type are fetched.
WHERE {list of predicates}
The list of predicates which are joined by AND OR NOT ( and ). They can
include named (or positioned) bind parameters. These parameters will need to
be bound by
Query#setParameter(String,Object).
ORDER BY {order by properties}
The list of properties to order the result. You can include ASC (ascending)
and DESC (descending) in the order by clause.
LIMIT {max rows} [ OFFSET {first row} ]
The limit offset specifies the max rows and first row to fetch. The offset is
optional.
Examples of Ebean's Query Language
Find orders fetching its id, shipDate and status properties. Note that the id
property is always fetched even if it is not included in the list of fetch
properties.
select (shipDate, status)
Find orders with a named bind variable (that will need to be bound via
Query#setParameter(String,Object)).
where customer.name like :custLike
Find orders and also fetch the customer with a named bind parameter. This
will fetch and populate both the order and customer objects.
fetch customer
Find orders and also fetch the customer, customer shippingAddress, order
details and related product. Note that customer and product objects will be
"Partial Objects" with only some of their properties populated. The customer
objects will have their id, name and shipping address populated. The product
objects (associated with each order detail) will have their id, sku and name
populated.
fetch customer (name)