package io.ebean; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.List; /** * Intended to be used as a base class for 'Finder' implementations that can then * be injected or used as public static fields on the associated entity bean. *
* These 'finders' are a place to organise all the finder methods for that bean type * and specific finder methods are expected to be added (find by unique properties etc). *
** For testing the mocki-ebean project has the ability to replace the finder implementation. *
*{@code
*
* public class CustomerFinder extends Finder {
*
* public CustomerFinder() {
* super(Customer.class);
* }
*
* // Add finder methods ...
*
* public Customer byName(String name) {
* return query().eq("name", name).findOne();
* }
*
* public List findNew() {
* return query().where()
* .eq("status", Customer.Status.NEW)
* .order("name")
* .findList()
* }
* }
*
* @Entity
* public class Customer extends BaseModel {
*
* public static final CustomerFinder find = new CustomerFinder();
* ...
*
* }
* }
* * When the Finder is registered as a field on Customer it can then be used like: *
*{@code
*
* Customer rob = Customer.find.byName("Rob");
*
* }
*
*/
public class Finder {
/**
* The entity bean type.
*/
private final Class{@code
*
* public class CustomerFinder extends Finder {
*
* public CustomerFinder() {
* super(Customer.class);
* }
*
* // ... add extra customer specific finder methods
* }
*
* @Entity
* public class Customer extends BaseModel {
*
* public static final CustomerFinder find = new CustomerFinder();
* ...
*
* }
* }
*/
public Finder(Class* This is equivalent to {@link DB#byName(String)} * * @param databaseName The name of the Database. If this is null then the default database is returned. */ public Database db(String databaseName) { return DB.byName(databaseName); } /** * Creates an entity reference for this ID. *
* Equivalent to {@link Database#reference(Class, Object)} */ @Nonnull public T ref(I id) { return db().reference(type, id); } /** * Retrieves an entity by ID. *
* Equivalent to {@link Database#find(Class, Object)} */ @Nullable public T byId(I id) { return db().find(type, id); } /** * Delete a bean by Id. *
* Equivalent to {@link Database#delete(Class, Object)}
*/
public void deleteById(I id) {
db().delete(type, id);
}
/**
* Retrieves all entities of the given type.
*/
@Nonnull
public List
* Equivalent to {@link Database#update(Class)}
*/
public UpdateQuery
* Equivalent to {@link Database#find(Class)}
*/
public Query{@code
*
* int rows =
* finder.update()
* .set("status", Customer.Status.ACTIVE)
* .set("updtime", new Timestamp(System.currentTimeMillis()))
* .where()
* .gt("id", 1000)
* .update();
*
* }
*
*