public interface ExtendedServer
This provides the finder methods that take an explicit transaction rather than obtaining the transaction from the usual mechanism (which is ThreadLocal based).
In general we only want to use this ExtendedServer API when we want to avoid / bypass the use of the mechanism to get the current transaction and instead explicitly supply the transaction to use.
Note that in all cases the transaction supplied can be null and in this case the EbeanServer will use the normal mechanism to obtain the transaction to use.
Modifier and Type | Method and Description |
---|---|
long |
clockNow()
Return the NOW time from the Clock.
|
<T> int |
delete(Query<T> query,
Transaction transaction)
Execute as a delete query deleting the 'root level' beans that match the predicates
in the query.
|
<T> int |
findCount(Query<T> query,
Transaction transaction)
Return the number of 'top level' or 'root' entities this query should return.
|
<T> void |
findEach(Query<T> query,
java.util.function.Consumer<T> consumer,
Transaction transaction)
Execute the query visiting the each bean one at a time.
|
void |
findEach(SqlQuery query,
java.util.function.Consumer<SqlRow> consumer,
Transaction transaction)
Execute the SqlQuery iterating a row at a time.
|
<T> void |
findEachWhile(Query<T> query,
java.util.function.Predicate<T> consumer,
Transaction transaction)
Execute the query visiting the each bean one at a time.
|
void |
findEachWhile(SqlQuery query,
java.util.function.Predicate<SqlRow> consumer,
Transaction transaction)
Execute the SqlQuery iterating a row at a time with the ability to stop consuming part way through.
|
<T> FutureRowCount<T> |
findFutureCount(Query<T> query,
Transaction transaction)
Execute find row count query in a background thread.
|
<T> FutureIds<T> |
findFutureIds(Query<T> query,
Transaction transaction)
Execute find Id's query in a background thread.
|
<T> FutureList<T> |
findFutureList(Query<T> query,
Transaction transaction)
Execute find list query in a background thread returning a FutureList object.
|
<A,T> List<A> |
findIds(Query<T> query,
Transaction transaction)
Return the Id values of the query as a List.
|
<T> QueryIterator<T> |
findIterate(Query<T> query,
Transaction transaction)
Return a QueryIterator for the query.
|
<T> List<T> |
findList(Query<T> query,
Transaction transaction)
Execute a query returning a list of beans.
|
List<SqlRow> |
findList(SqlQuery query,
Transaction transaction)
Execute the sql query returning a list of MapBean.
|
<K,T> Map<K,T> |
findMap(Query<T> query,
Transaction transaction)
Execute the query returning the entity beans in a Map.
|
<T> T |
findOne(Query<T> query,
Transaction transaction)
Execute the query returning at most one entity bean or null (if no matching
bean is found).
|
SqlRow |
findOne(SqlQuery query,
Transaction transaction)
Execute the sql query returning a single MapBean or null.
|
<T> Optional<T> |
findOneOrEmpty(Query<T> query,
Transaction transaction)
Similar to findOne() but returns an Optional (rather than nullable).
|
<T> PagedList<T> |
findPagedList(Query<T> query,
Transaction transaction)
Return a PagedList for this query using firstRow and maxRows.
|
<T> Set<T> |
findSet(Query<T> query,
Transaction transaction)
Execute the query returning a set of entity beans.
|
<A,T> List<A> |
findSingleAttributeList(Query<T> query,
Transaction transaction)
Execute the query returning a list of values for a single property.
|
<T> List<Version<T>> |
findVersions(Query<T> query,
Transaction transaction)
Return versions of a @History entity bean.
|
void |
setClock(java.time.Clock clock)
Set the Clock to use for
@WhenCreated and @WhenModified . |
<T> int |
update(Query<T> query,
Transaction transaction)
Execute the update query returning the number of rows updated.
|
long clockNow()
void setClock(java.time.Clock clock)
@WhenCreated
and @WhenModified
.
Note that we only expect to change the Clock for testing purposes.
<T> int findCount(Query<T> query, Transaction transaction)
Query.findCount()
,
Query.findFutureCount()
@Nonnull <A,T> List<A> findIds(Query<T> query, Transaction transaction)
Query.findIds()
@Nonnull <T> QueryIterator<T> findIterate(Query<T> query, Transaction transaction)
Generally using findEach(Query, Consumer, Transaction)
or
findEachWhile(Query, Predicate, Transaction)
is preferred
to findIterate(). The reason is that those methods automatically take care of
closing the queryIterator (and the underlying jdbc statement and resultSet).
This is similar to findEach in that not all the result beans need to be held in memory at the same time and as such is good for processing large queries.
<T> void findEach(Query<T> query, java.util.function.Consumer<T> consumer, Transaction transaction)
Unlike findList() this is suitable for processing a query that will return a very large resultSet. The reason is that not all the result beans need to be held in memory at the same time and instead processed one at a time.
Internally this query using a PersistenceContext scoped to each bean (and the beans associated object graph).
ebeanServer.find(Order.class)
.where().eq("status", Order.Status.NEW)
.order().asc("id")
.findEach((Order order) -> {
// do something with the order bean
System.out.println(" -- processing order ... " + order);
});
<T> void findEachWhile(Query<T> query, java.util.function.Predicate<T> consumer, Transaction transaction)
Compared to findEach() this provides the ability to stop processing the query results early by returning false for the Predicate.
Unlike findList() this is suitable for processing a query that will return a very large resultSet. The reason is that not all the result beans need to be held in memory at the same time and instead processed one at a time.
Internally this query using a PersistenceContext scoped to each bean (and the beans associated object graph).
ebeanServer.find(Order.class)
.where().eq("status", Order.Status.NEW)
.order().asc("id")
.findEachWhile((Order order) -> {
// do something with the order bean
System.out.println(" -- processing order ... " + order);
boolean carryOnProcessing = ...
return carryOnProcessing;
});
@Nonnull <T> List<Version<T>> findVersions(Query<T> query, Transaction transaction)
Generally this query is expected to be a find by id or unique predicates query. It will execute the query against the history returning the versions of the bean.
@Nonnull <T> List<T> findList(Query<T> query, Transaction transaction)
Generally you are able to use Query.findList()
rather than
explicitly calling this method. You could use this method if you wish to
explicitly control the transaction used for the query.
List<Customer> customers =
ebeanServer.find(Customer.class)
.where().ilike("name", "rob%")
.findList();
T
- the type of entity bean to fetch.query
- the query to execute.transaction
- the transaction to use (can be null).Query.findList()
@Nonnull <T> FutureRowCount<T> findFutureCount(Query<T> query, Transaction transaction)
This returns a Future object which can be used to cancel, check the execution status (isDone etc) and get the value (with or without a timeout).
query
- the query to execute the row count ontransaction
- the transaction (can be null).Query.findFutureCount()
@Nonnull <T> FutureIds<T> findFutureIds(Query<T> query, Transaction transaction)
This returns a Future object which can be used to cancel, check the execution status (isDone etc) and get the value (with or without a timeout).
query
- the query to execute the fetch Id's ontransaction
- the transaction (can be null).Query.findFutureIds()
@Nonnull <T> FutureList<T> findFutureList(Query<T> query, Transaction transaction)
This returns a Future object which can be used to cancel, check the execution status (isDone etc) and get the value (with or without a timeout).
This query will execute in it's own PersistenceContext and using its own transaction. What that means is that it will not share any bean instances with other queries.
query
- the query to execute in the backgroundtransaction
- the transaction (can be null).Query.findFutureList()
@Nonnull <T> PagedList<T> findPagedList(Query<T> query, Transaction transaction)
The benefit of using this over findList() is that it provides functionality to get the total row count etc.
If maxRows is not set on the query prior to calling findPagedList() then a PersistenceException is thrown.
PagedList<Order> pagedList = Ebean.find(Order.class)
.setFirstRow(50)
.setMaxRows(20)
.findPagedList();
// fetch the total row count in the background
pagedList.loadRowCount();
List<Order> orders = pagedList.getList();
int totalRowCount = pagedList.getTotalRowCount();
Query.findPagedList()
@Nonnull <T> Set<T> findSet(Query<T> query, Transaction transaction)
Generally you are able to use Query.findSet()
rather than
explicitly calling this method. You could use this method if you wish to
explicitly control the transaction used for the query.
Set<Customer> customers =
ebeanServer.find(Customer.class)
.where().ilike("name", "rob%")
.findSet();
T
- the type of entity bean to fetch.query
- the query to executetransaction
- the transaction to use (can be null).Query.findSet()
@Nonnull <K,T> Map<K,T> findMap(Query<T> query, Transaction transaction)
Generally you are able to use Query.findMap()
rather than
explicitly calling this method. You could use this method if you wish to
explicitly control the transaction used for the query.
T
- the type of entity bean to fetch.query
- the query to execute.transaction
- the transaction to use (can be null).Query.findMap()
@Nonnull <A,T> List<A> findSingleAttributeList(Query<T> query, Transaction transaction)
List<String> names =
Ebean.find(Customer.class)
.select("name")
.orderBy().asc("name")
.findSingleAttributeList();
List<String> names =
Ebean.find(Customer.class)
.setDistinct(true)
.select("name")
.where().eq("status", Customer.Status.NEW)
.orderBy().asc("name")
.setMaxRows(100)
.findSingleAttributeList();
Query.findSingleAttributeList()
@Nullable <T> T findOne(Query<T> query, Transaction transaction)
This will throw a NonUniqueResultException if the query finds more than one result.
Generally you are able to use Query.findOne()
rather than
explicitly calling this method. You could use this method if you wish to
explicitly control the transaction used for the query.
T
- the type of entity bean to fetch.query
- the query to execute.transaction
- the transaction to use (can be null).javax.persistence.NonUniqueResultException
- if more than one result was foundQuery.findOne()
@Nonnull <T> Optional<T> findOneOrEmpty(Query<T> query, Transaction transaction)
<T> int delete(Query<T> query, Transaction transaction)
Note that if the query includes joins then the generated delete statement may not be optimal depending on the database platform.
T
- the type of entity bean to fetch.query
- the query used for the deletetransaction
- the transaction to use (can be null)<T> int update(Query<T> query, Transaction transaction)
The update query must be created using EbeanServer.update(Class)
.
T
- the type of entity beanquery
- the update query to executetransaction
- the optional transaction to use for the update (can be null)@Nonnull List<SqlRow> findList(SqlQuery query, Transaction transaction)
Generally you are able to use SqlQuery.findList()
rather than
explicitly calling this method. You could use this method if you wish to
explicitly control the transaction used for the query.
query
- the query to execute.transaction
- the transaction to use (can be null).SqlQuery.findList()
void findEach(SqlQuery query, java.util.function.Consumer<SqlRow> consumer, Transaction transaction)
This streaming type query is useful for large query execution as only 1 row needs to be held in memory.
void findEachWhile(SqlQuery query, java.util.function.Predicate<SqlRow> consumer, Transaction transaction)
Returning false after processing a row stops the iteration through the query results.
This streaming type query is useful for large query execution as only 1 row needs to be held in memory.
@Nullable SqlRow findOne(SqlQuery query, Transaction transaction)
This will throw a PersistenceException if the query found more than one result.
Generally you are able to use SqlQuery.findOne()
rather than
explicitly calling this method. You could use this method if you wish to
explicitly control the transaction used for the query.
query
- the query to execute.transaction
- the transaction to use (can be null).SqlQuery.findOne()
Copyright © 2018. All rights reserved.