All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ebean.BeanFinder Maven / Gradle / Ivy

package io.ebean;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;

/**
 * Provides finder functionality for use with "Dependency Injection style" use of Ebean.
 * 

* Note that typically users would extend BeanRepository rather than BeanFinder. *

*
{@code
 *
 * public class CustomerFinder extends BeanFinder {
 *
 *   @Inject
 *   public CustomerFinder(EbeanServer server) {
 *     super(Customer.class, server);
 *   }
 *
 *   // ... add customer specific finders
 * }
 *
 * }
* * @param The ID type * @param The Bean type */ public abstract class BeanFinder { protected final EbeanServer server; protected final Class type; /** * Create with the given bean type and EbeanServer instance. * * @param type The bean type * @param server The EbeanServer instance typically created via Spring factory or equivalent. */ protected BeanFinder(Class type, EbeanServer server) { this.type = type; this.server = server; } /** * Return the EbeanServer to use. */ public EbeanServer db() { return server; } /** * Return the current transaction. */ public Transaction currentTransaction() { return db().currentTransaction(); } /** * Flush the JDBC batch on the current transaction. */ public void flush() { db().flush(); } /** * Return typically a different EbeanServer to the default. *

* This is equivalent to {@link Ebean#getServer(String)} * * @param server The name of the EbeanServer. If this is null then the default EbeanServer is * returned. */ public EbeanServer db(String server) { return Ebean.getServer(server); } /** * Creates an entity reference for this ID. *

* Equivalent to {@link EbeanServer#getReference(Class, Object)} */ @Nonnull public T ref(I id) { return db().getReference(type, id); } /** * Retrieves an entity by ID. *

* Equivalent to {@link EbeanServer#find(Class, Object)} */ @Nullable public T findById(I id) { return db().find(type, id); } /** * Find an entity by ID returning an Optional. */ @Nullable public Optional findByIdOrEmpty(I id) { return db().find(type).setId(id).findOneOrEmpty(); } /** * Delete a bean by Id. *

* Equivalent to {@link EbeanServer#delete(Class, Object)} */ public void deleteById(I id) { db().delete(type, id); } /** * Retrieves all entities of the given type. */ @Nonnull public List findAll() { return query().findList(); } /** * Creates an update query. * *

{@code
   *
   *  int rows =
   *      updateQuery()
   *      .set("status", Customer.Status.ACTIVE)
   *      .set("updtime", new Timestamp(System.currentTimeMillis()))
   *      .where()
   *        .gt("id", 1000)
   *        .update();
   *
   * }
* *

* Equivalent to {@link EbeanServer#update(Class)} */ protected UpdateQuery updateQuery() { return db().update(type); } /** * Creates a query. *

* Equivalent to {@link EbeanServer#find(Class)} */ protected Query query() { return db().find(type); } /** * Creates a native sql query. */ protected Query nativeSql(String nativeSql) { return db().findNative(type, nativeSql); } /** * Creates a query using the ORM query language. */ protected Query query(String ormQuery) { return db().createQuery(type, ormQuery); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy