Module io.ebean.api
Package io.ebean

Class Finder<I,​T>


  • @NonNullApi
    public class Finder<I,​T>
    extends Object
    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.

    {@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()
       }
     }
    See Also:
    BeanRepository, BeanFinder
    • Constructor Detail

      • Finder

        public Finder​(Class<T> type)
        Create with the type of the entity bean.
        {@code
        
         public class CustomerFinder extends Finder {
        
           public CustomerFinder() {
             super(Customer.class);
           }
        
           // ... add extra customer specific finder methods
         }
      • Finder

        public Finder​(Class<T> type,
                      String databaseName)
        Create with the type of the entity bean and specific database name.
    • Method Detail

      • currentTransaction

        public Transaction currentTransaction()
        Return the current transaction.
      • flush

        public void flush()
        Flush the JDBC batch on the current transaction.
      • db

        public Database db()
        Return the Database this finder will use.
      • db

        public Database db​(String databaseName)
        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.
      • all

        public List<T> all()
        Retrieves all entities of the given type.
      • update

        public UpdateQuery<T> 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)

      • nativeSql

        public Query<T> nativeSql​(String nativeSql)
        Creates a native sql query.
      • query

        public Query<T> query​(String ormQuery)
        Creates a query using the ORM query language.