public class Finder<I,T> extends Object
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.
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)
.orderBy("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");
Constructor and Description |
---|
Finder(Class<T> type)
Create with the type of the entity bean.
|
Finder(Class<T> type,
String databaseName)
Create with the type of the entity bean and specific database name.
|
Modifier and Type | Method and Description |
---|---|
List<T> |
all()
Retrieves all entities of the given type.
|
T |
byId(I id)
Retrieves an entity by ID.
|
Transaction |
currentTransaction()
Return the current transaction.
|
Database |
db()
Return the Database this finder will use.
|
Database |
db(String databaseName)
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.
|
Query<T> |
nativeSql(String nativeSql)
Creates a native sql query.
|
Query<T> |
query()
Creates a query.
|
Query<T> |
query(String ormQuery)
Creates a query using the ORM query language.
|
T |
ref(I id)
Creates an entity reference for this ID.
|
UpdateQuery<T> |
update()
Creates an update query.
|
public Finder(Class<T> type)
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();
...
public Transaction currentTransaction()
public void flush()
public Database db(String databaseName)
This is equivalent to DB.byName(String)
databaseName
- The name of the Database. If this is null then the default database is returned.@Nonnull public T ref(I id)
Equivalent to Database.getReference(Class, Object)
@Nullable public T byId(I id)
Equivalent to Database.find(Class, Object)
public void deleteById(I id)
Equivalent to Database.delete(Class, Object)
public UpdateQuery<T> update()
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)
public Query<T> query()
Equivalent to Database.find(Class)
Copyright © 2019. All rights reserved.