java.lang.Object
io.ebean.Finder<I,T>
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.
When using dependency injection BeanRepository
and BeanFinder
are expected to be used rather than this Finder.
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).
Testing
For testing the mocki-ebean project has the ability to replace the finder implementation.
public class CustomerFinder extends Finder<Long,Customer> {
public CustomerFinder() {
super(Customer.class);
}
// Add finder methods ...
public Customer byName(String name) {
return query().eq("name", name).findOne();
}
public List<Customer> 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:
Customer rob = Customer.find.byName("Rob");
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionall()
Retrieves all entities of the given type.Retrieves an entity by ID.Return the current transaction.db()
Return the Database this finder will use.Return typically a different Database to the default.void
deleteById
(I id) Delete a bean by Id.void
flush()
Flush the JDBC batch on the current transaction.Creates a native sql query.query()
Creates a query.Creates an entity reference for this ID.update()
Creates an update query.
-
Constructor Details
-
Finder
Create with the type of the entity bean.public class CustomerFinder extends Finder<Customer> { public CustomerFinder() { super(Customer.class); } // ... add extra customer specific finder methods } @Entity public class Customer extends BaseModel { public static final CustomerFinder find = new CustomerFinder(); ... }
-
Finder
Create with the type of the entity bean and specific database name.
-
-
Method Details
-
currentTransaction
Return the current transaction. -
flush
public void flush()Flush the JDBC batch on the current transaction. -
db
Return the Database this finder will use. -
db
Return typically a different Database to the default.This is equivalent to
DB.byName(String)
- Parameters:
databaseName
- The name of the Database. If this is null then the default database is returned.
-
ref
Creates an entity reference for this ID.Equivalent to
Database.reference(Class, Object)
-
byId
Retrieves an entity by ID.Equivalent to
Database.find(Class, Object)
-
deleteById
Delete a bean by Id.Equivalent to
Database.delete(Class, Object)
-
all
Retrieves all entities of the given type. -
update
Creates an update query.int rows = finder.update() .set("status", Customer.Status.ACTIVE) .set("updtime", new Timestamp(System.currentTimeMillis())) .where() .gt("id", 1000) .update();
Equivalent to
Database.update(Class)
-
query
Creates a query.Equivalent to
Database.find(Class)
-
nativeSql
Creates a native sql query.
-