Related: Trouble Shooting

Finder

We can use the ebeaninit command line tool to generate Finders for us.

We can create and use a Finder for each of our entity beans, for example a CustomerFinder for the Customer entity bean. We do this to organise our code grouping all the "finder logic" for a given bean type into it's finder (so that everyone knows where it is).

e.g. CustomerFinder

A CustomerFinder generated by the ebeaninit command line tool.

package org.example.domain.finder;

import io.ebean.Finder;
import org.example.domain.Customer;

public class CustomerFinder extends Finder<Long,Customer> {

  /**
   * Construct using the default EbeanServer.
   */
  public CustomerFinder() {
    super(Customer.class);
  }

}

When we generate Finders these are also linked to the entity via a public field. We can use ebean-mocker when we want to mock or stub the finders for testing purposes.

Linked CustomerFinder

...
@Entity
public class Customer extends BaseModel {

  // finder linked as a public static field
  public static final CustomerFinder find = new CustomerFinder();

Example Finder use

With the CustomerFinder linked as a field to Customer we can then easily use it.

// find by id
Customer customer = Customer.find.byId(42L);

// reference bean
Customer refBean = Customer.find.ref(42L);

List<Customer> customers =
  Customer.find
    .where().istartsWith("name", "Rob")
    .findList();

// generally it is recommended that we move "find logic" for Customer
// into the CustomerFinder so instead we should create a "byName" method
// on CustomerFinder and end up with:

List<Customer> customers = Customer.find.byName("Rob");

Finder vs Repository

We don't have to use Finders to organise our code. The common alternative would be to use the repository pattern and have a CustomerRepository instead and have all the "finder logic" for customer in CustomerRepository.

The recommended approach is to use Model and Finder rather than the Repository pattern.

Edit Page